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

考试成绩的前三名: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at HelloWorld.show(HelloWorld.java:19) at HelloWorld.main(HelloWorld.java:11)

import java.util.Arrays;



public class HelloWorld {

    

    //完成 main 方法

    public static void main(String[] args) {

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

        System.out.println("考试成绩的前三名:");

        HelloWorld  grade=new HelloWorld();

        grade.show(scores);

}

    

    //定义方法完成成绩排序并输出前三名的功能

    public void  show(int[] scores){

        Arrays.sort(scores);

        int num=0;

        for(int i=scores.length-1;i>=0;i++){

            if(scores[i]<0||scores[i]>100){

                continue;

            }num++;

            if(num>3){

                break;

            }System.out.println(scores[i]);

        }

    }

    


正在回答

4 回答

有4个是输出换行标签

beczr

0 回复 有任何疑惑可以回复我~

for循环里应该是i--,而不是i++

0 回复 有任何疑惑可以回复我~
import java.util.Arrays;
public class HelloWorld {
    
    //完成 main 方法
    public static void main(String[] args) {
        int[] scores={89 , -23 , 64 , 91 , 119 , 52 , 73};
        HelloWorld.getMax(scores);
        
        
    }
    
    //定义方法完成成绩排序并输出前三名的功能
    
    public static void getMax(int[] scores){
        Arrays.sort(scores);
        int count=0;//统计有效成绩的前三名的数量;
        for(int i=scores.length-1;i>0;i--){
            if(scores[i]<0||scores[i]>100){
                continue;
            }
            else{
                count++;
                System.out.println(scores[i]);
            }
            if(count>=3){
                break;
            }
        }
    }


0 回复 有任何疑惑可以回复我~

哥们儿,

for(int i=scores.length-1;i>=0;i++){

这个地方是什么意思?你再仔细看看

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

考试成绩的前三名: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at HelloWorld.show(HelloWorld.java:19) at HelloWorld.main(HelloWorld.java:11)

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信