package com.imooc;public class helloWorld { //完成 main 方法 public static void main(String[] args) { // 创建对象,对象名为hello 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; } }}
1 回答
已采纳
阿旭_
TA贡献217条经验 获得超353个赞
方法不能嵌套方法:即:main()不能嵌套getMaxAge()
换好位置,结果如下:
package com.imooc;
public class helloWorld {
// 完成 main 方法
public static void main(String[] args) {
// 创建对象,对象名为hello
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;
}
}
添加回答
举报
0/150
提交
取消
