import java.util.Arrays;
public class Demo{
public static void main(String[] args){
int[] scores={89,-23,64,91,119,52,73}
Demo hello=new Demo();
System.out.println("考试成绩的前三名为:");
hello.getScores(scores);
}
public int[] getScores(scores){
Arrays.sort(scores);
int count=0;
if(count<3){
for(int i=scores.length-1;i--){
if(scores[i]<0||scores[i]>100){
continue;
}else{
System.out.println(scores[i]);
count++;
}
}
}
}
代码要实现输出考试成绩的前三名。
}
3 回答
已采纳
绿洲仙人球
TA贡献39条经验 获得超47个赞
循环怎么嵌套应该和需求有关系,我读了一下题主的代码,就贴出来的这部分而言是无法运行的,一是代码语法有点错误,而是根据需求,要输出考试成绩的前三名,代码的逻辑也有点问题,在没有大改动题主的代码的基础上,实现了功能,代码如下
public class Demo {
public static void main(String[] args) {
int[] scores = {89, -23, 64, 91, 119, 52, 73 };
Demo hello = new Demo();
System.out.println("考试成绩的前三名为:");
hello.getScores(scores);
}
public void getScores(int[] scores) {
Arrays.sort(scores);
int count = 0;
for (int i = scores.length - 1;; i--) {
if (count < 3) {
if (scores[i] < 0 || scores[i] > 100) {
continue;
} else {
System.out.println(scores[i]);
count++;
}
}
}
}
}需要把for循环和if (count < 3) 的判断调换一下位置,这样就没有问题了
qq_匡璐_0
TA贡献96条经验 获得超96个赞
public void main(String[] args) {
int[] scores = {89, -23, 64, 91, 119, 52, 73};
Arrays.sort(scores);
System.out.println("考试成绩的前三名为:");
for(int i = 0 ;i<3;i++){
int j = scores[scores.length-1-i];
System.out.println(j);
}
}
添加回答
举报
0/150
提交
取消
