为了账号安全,请及时绑定邮箱和手机立即绑定

请大家帮我看看错在哪里了~谢谢

请大家帮我看看错在哪里了~谢谢

kathria 2016-09-07 15:54:10
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;
	}
}

//img1.sycdn.imooc.com//57cfccf80001305d02930104.jpg

查看完整回答
2 反对 回复 2016-09-07
  • 1 回答
  • 0 关注
  • 1068 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信