计算平均值时意外的 return 值?
unexpected return value when computing average?
我无法在 getter 方法中 return "average" 变量的值。我一直收到错误 "incompatible types: unexpected return value" 但该值是在上面的 setter 方法中定义的?
我寻找了类似的问题,但找不到与此相近的问题
public class Student {
private String firstName;
private String lastName;
private int score1, score2, score3;
private int average;
public void setFirstName(String name){
firstName = name;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String name){
lastName = name;
}
public String getLastName(){
return lastName;
}
public void setScore1(String newvalue){
score1 = Integer.parseInt(newvalue);
}
public int getScore1(int newvalue){
return score1;
}
public void setScore2(String newvalue){
score2 = Integer.parseInt(newvalue);
}
public int getScore2(int newvalue){
return score2;
}
public void setScore3(String newvalue){
score3 = Integer.parseInt(newvalue);
}
public int getScore3(int newvalue){
return score3;
}
public int setAverage(int newvalue){
average = (score1 + score2 + score3)/3;
}
public void getAverage(String newvalue){
return average;
}
}
因为您的 getAverage(String newvalue)
方法有一个由 return 类型组成的函数定义 void
并且您正在 return 从该方法中获取值。
Any method having return type set as void in the function
definition/declaration isn't supposed to return any value.
请更正为:-
public int getAverage(String newvalue){
return average;
我无法在 getter 方法中 return "average" 变量的值。我一直收到错误 "incompatible types: unexpected return value" 但该值是在上面的 setter 方法中定义的?
我寻找了类似的问题,但找不到与此相近的问题
public class Student {
private String firstName;
private String lastName;
private int score1, score2, score3;
private int average;
public void setFirstName(String name){
firstName = name;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String name){
lastName = name;
}
public String getLastName(){
return lastName;
}
public void setScore1(String newvalue){
score1 = Integer.parseInt(newvalue);
}
public int getScore1(int newvalue){
return score1;
}
public void setScore2(String newvalue){
score2 = Integer.parseInt(newvalue);
}
public int getScore2(int newvalue){
return score2;
}
public void setScore3(String newvalue){
score3 = Integer.parseInt(newvalue);
}
public int getScore3(int newvalue){
return score3;
}
public int setAverage(int newvalue){
average = (score1 + score2 + score3)/3;
}
public void getAverage(String newvalue){
return average;
}
}
因为您的 getAverage(String newvalue)
方法有一个由 return 类型组成的函数定义 void
并且您正在 return 从该方法中获取值。
Any method having return type set as void in the function definition/declaration isn't supposed to return any value.
请更正为:-
public int getAverage(String newvalue){
return average;