atom - 麻烦看下这段java代码出了什么问题
迷茫
迷茫 2017-04-17 14:54:19
[Java讨论组]

import java.util.Arrays;

public class mark {

    public static void main(String[] args) {

        int [] scores = {89,-23,64,91,119,52,73};

        mark hello = new mark();

        hello.handleGrade(scores);
    }
    public void handleGrade(int [] a){
        
        int num = 0;

        int [] three = new int [3];

        Arrays.sort(a);

        for (int i = a.length-1;i >= 0;i--) {
            if (a[i] > 100 || a[i] < 0) {
                continue;
            }
            else{
                three[num] = a[i];
                num++;
            }
            if (num==3) {
                    System.out.println("考试成绩的前三名 : " + Arrays.toString(three));
            }
        }
    }
}
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(3)
怪我咯


这里执行完了,怎么不退出循环呢?这不是等着越界吗?加上break,或者return

巴扎黑

贴报错信息,要么贴代码,你这样我也没法调试。

PHPz

handleGrade方法里,循环的次数时a数组的个数,但你的three数组初始化大小是3,没有跳出循环的地方,所以第4次循环会数组越界

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号