import java.util.Arrays;
public class HelloWorld {
//完成 main 方法
public static void main(String[] args) {
HelloWorld shuchu=new HelloWorld();
int scores[]={89,-23,64,91,119,52,73};
shuchu.chengji(scores);
}
//定义方法完成成绩排序并输出前三名的功能
public void chengji(int scores[])
{
int count=0;
Arrays.sort(scores);
for(int j=scores.length-1;j>=0&&count>=3;j--)
{
if(scores[j]<0||scores[j]>100)
continue;
System.out.println(scores[j]);
count++;
}
}
}for()循环语句里面的判断条件有错。count是小于3,而不是count >= 3; 因为for语句一开始执行就结束了,count = 0,不满足条件。为什么不能等于3呢,因为count 是从零开始的,只需要输出三个数就好,count最大为2.
用Eclipse软件,可以出现结果,结果也是对的,但是在这里就不能显示结果,求指明
package com.immoc;
public class HelloWorld {
//完成 main 方法
public static void main(String[] args) {
int[] scores={89,-23,64,91,119,52,73};
HelloWorld hello=new HelloWorld();
hello.get(scores);
}
//定义方法完成成绩排序并输出前三名的功能
public void get(int[] score){
System.out.println("考试成绩的前三名为: ");
for(int i=0;i<score.length-1;i++){
for(int j=i+1;j<score.length;j++){
if(score[i]<score[j]){
int t;
t=score[i];
score[i]=score[j];
score[j]=t;
}
}
}
int j=0;
for(int i=0;i<score.length;i++){
if((0<score[i])&&(score[i]<100)){
score[j]=score[i];
j++;
}
}
for(int i=0;i<3;i++){
System.out.println(score[i]);
}
}
}