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

我的代码错在什么地方呢?需要怎么改进?

我的代码错在什么地方呢?需要怎么改进?

慕尼黑2231714 2017-12-02 16:05:19
public class HelloWorld {        //完成 main 方法    public static void main(String[] args) {                        // 创建对象,对象名为hello HelloWorld hello = new HelloWorld();                 // 调用方法并将返回值保存在变量中        int maxScore = hello.getMaxAge();         // 输出最大年龄 System.out.println("最大年龄为:" + maxScore);  } /* * 功能:输出学生年龄的最大值      * 定义一个无参的方法,返回值为年龄的最大值     * 参考步骤:     * 1、定义一个整形数组 ages ,保存学生年龄,数组元素依次为 18 ,23 ,21 ,19 ,25 ,29 ,17     * 2、定义一个整形变量 max ,保存学生最大年龄,初始时假定数组中的第一个元素为最大值     * 3、使用 for 循环遍历数组中的元素,并与假定的最大值比较,如果比假定的最大值要大,则替换当前的最大值     * 4、使用 return 返回最大值 */ public int getMaxAge() {    int [] age = {18,23,21,19,25,29,17};        int max = age[0];        for(int i = 1;i<=age.length;i++)        {            if(age[i]>max)            max = age[i];        }        return max;         }}
查看完整描述

2 回答

?
遥望星辰_

TA贡献119条经验 获得超85个赞

for(int i = 1;i<=age.length;i++)改为for(int i = 1;i<age.length;i++),=就越界了

查看完整回答
反对 回复 2017-12-02
  • 2 回答
  • 0 关注
  • 1308 浏览

添加回答

举报

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