快捷导航

运行后只显示"考试成绩的前三名为:”;为什么没有成绩输出呢?

import java.util.Arrays;/*** 实现输出考试成绩的前三名* 要求:* 1、 考试成绩已保存在数组 scores 中,数组元素依次为 89 , -23 , 64 , 91 , 119 , 52 , 73* 2、 要求通过自定义方法来实现成绩排名并输出操作,将成绩数组作为参数传入* 3、 要求判断成绩的有效性( 0—100 ),如果成绩无效,则忽略此成绩*/public class HelloWorld {        // 完成 main 方法        public static void main(String[] args) {                int[] scores = { 89, -23, 64, 91, 119, 52, 73 };// 成绩数组                System.out.println("考试成绩的前三名为:");                HelloWorld hello = new HelloWorld();// hello对象                hello.scs(scores);// 调用scs方法        }        // 定义方法完成成绩排序并输出前三名的功能        public void scs(int[] scores) {                int count = 3;                Arrays.sort(scores);                for (int i = scores.length - 1; count > 0; i++) {                        if (scores > 100 || scores < 0) {// 不满足条件1-100的成绩跳出                                continue;                        } else {                                count--;                                System.out.println(scores);// 依次输出前三名成绩                        }                }        }}

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员2

错误位置在csc函数的for循环中..for (int i = scores.length - 1; count > 0; i++) {}
你是从后往前遍历的...不应该i++...i++会出现越界的...改成i--即可
//定义方法完成成绩排序并输出前三名的功能        publicvoidscs(int[]scores){                intcount=3;                Arrays.sort(scores);                for(inti=scores.length-1;count>=0;i--){                        if(scores>100||scores= 0 下标是从0开始的...你不等于0就会少对一个元素进行判断哦</p>
回复

使用道具 举报


原来是这样,(*^__^*) 嘻嘻……谢谢
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP