gpt4 book ai didi

java - 计算平均值时意外的返回值?

转载 作者:行者123 更新时间:2023-11-30 08:16:32 26 4
gpt4 key购买 nike

我无法在我的 getter 方法中返回“平均”变量的值。我一直收到错误“不兼容的类型:意外的返回值”,但该值是在上面的 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) 方法有一个函数定义,其返回类型为 void,并且您要从该方法返回值。

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;

关于java - 计算平均值时意外的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com