英文:
Pass named parameter to a method
问题
class AllTheColorsOfTheRainbow {
private int hue = 0;
int anIntegerRepresentingColors;
void changeTheHueOfTheColor(int newHue) {
this.hue = newHue;
}
public int getHue(){
return this.hue;
}
}
public class Ex11 {
public static void main(String [] args){
AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow();
a.changeTheHueOfTheColor(newHue = 1);
System.out.println(a.getHue());
}
}
Stack trace:
javac Ex11.java
Ex11.java:18: error: cannot find symbol
a.changeTheHueOfTheColor(newHue = 1);
^
symbol: variable newHue
location: class Ex11
1 error
What does this mean, and how can I correct it?
英文:
Code:
class AllTheColorsOfTheRainbow {
private int hue = 0;
int anIntegerRepresentingColors;
void changeTheHueOfTheColor(int newHue) {
this.hue = newHue;
}
public int getHue(){
return this.hue;
}
}
public class Ex11 {
public static void main(String [] args){
AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow();
a.changeTheHueOfTheColor(newHue = 1);
System.out.println(a.getHue());
}
}
Stack trace:
javac Ex11.java
Ex11.java:18: error: cannot find symbol
a.changeTheHueOfTheColor(newHue = 1);
^
symbol: variable newHue
location: class Ex11
1 error
What does this mean, and how can I correct it?
答案1
得分: 5
Java没有命名参数,只有位置参数。您需要在不带参数名称的情况下传递它:
a.changeTheHueOfTheColor(1);
// 这里 -----------------^
英文:
Java does not have named arguments, just positional arguments. You need to pass it without the parameter's name:
a.changeTheHueOfTheColor(1);
// Here -----------------^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论