package xunhuan;import java.util.Arrays;public class qiansanm { //完成 main 方法 public static void main(String[] args) { qiansanm hello=new qiansanm(); //定义有效前三名的变量 System.out.println("请输入前三名的学员成绩"); int[] scores={89,-23,64,91,119,52,73}; Arrays.sort(scores); hello.nums(scores); } //定义方法完成成绩排序并输出前三名的功能 public int[] nums(int[] scores){ int one = 0; for(int i=0; i<scores.length;i++){ if(scores[i]<0||scores[i]>100){ continue; } one++; if(one>=3){ System.out.println( scores[i]); } return score; } }
1 回答
一条小咸鱼
TA贡献457条经验 获得超255个赞
public int[] nums(int[] scores) {
int one = 0;
for (int i = 0; i < scores.length; i++) {
if (scores[i] < 0 || scores[i] > 100) {
continue;
}
one++;
if (one >= 3) {
System.out.println(scores[i]);
}
}
return scores;
}其实你这个程序逻辑有问题,你随意加一个0-100的数,就知道不对了,应该反向输出
这是我写的,你可以参考下
import java.util.Arrays;
public class HelloWorld {
// 完成 main 方法
public static void main(String[] args) {
int[] scores = { 89, -23, 64, 119, 52, 73 };
HelloWorld hello = new HelloWorld();
hello.scoSortAndPrint(scores);
}
// 定义方法完成成绩排序并输出前三名的功能
public void scoSortAndPrint(int[] score) {
Arrays.sort(score);
int count = 0;
System.out.println("考试成绩的前三名为: ");
for (int i = score.length - 1; i >= 0 && count <3; i--) {
if (score[i] <= 100 && score[i] >= 0) {
System.out.println(score[i]);
count++;
}
}
}
}添加回答
举报
0/150
提交
取消
