public class helloworld { //完成 main 方法 public static void main(String[] args) { helloworld hello = new helloworld(); int maxScore=hello.getMaxAge(); // 输出最大年龄 System.out.println("最大年龄为:" + maxScore); }public int getMaxAge(){ 错误原因:Multiple markers at this line - This method must return a result of type int - This method must return a result of type int int []ages={18,23,21,19,25,29,17}; int max=ages[0]; for(int i=0;i<ages.length;i++){ if(ages[i]>max); max=ages[i] ; return max; }哪里错了呢,怎么修改
3 回答
已采纳
Bran_Zuo
TA贡献3条经验 获得超1个赞
for循环里执行return只是退出循环,但是getMaxAge方法并没有返回语句,应将return语句放在for循环外
public class helloworld {
public static void main(String[] args)
{
helloworld hello = new helloworld();
int maxScore=hello.getMaxAge();
// 输出最大年龄
System.out.println("最大年龄为:" + maxScore);
}
public int getMaxAge()
{
int []ages={18,23,21,19,25,29,17};
int max=ages[0];
for(int i=0;i<ages.length;i++)
{
if(ages[i]>max);
max=ages[i] ;
}
return max;
}
}
Its_forever
TA贡献361条经验 获得超328个赞
return max;放到括号之后。
public int getMaxAge(){
int []ages={18,23,21,19,25,29,17};
int max=ages[0];
for(int i=0;i<ages.length;i++){
if(ages[i]>max);
max=ages[i] ;
}
return max;
}添加回答
举报
0/150
提交
取消
