問題已解決
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[] scores = {89,-23,64,91,119,52,73};
? ? ? ? System.out.println("考試成績(jī)的前三名為");
? ? ? ? HelloWorld hello = new HelloWorld();
? ? ? ? hello.export(scores);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績(jī)排序并輸出前三名的功能
? ??
? ??
? ? public void export(int [] scores){
? ? ? ? int count=0;
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ? ? if(scores[i]<0 && scores[i]>100){
? ? ? ? ? ? ? ? continue;
}
? ? ? ? ? ? ?
? ? ? ? ? ? ?count=+1;
? ? ? ? ? ? ?if(count == 3){
? ? ? ? ? ? ?break;
? ? ? ? ? ? }
System.out.println(scores[i]);
? ? ? ? }
? ? }
}
2019-03-04
錯(cuò)誤有三處:
if(scores[i]<0 && scores[i]>100) 邏輯運(yùn)算錯(cuò)誤,此處應(yīng)該使用||運(yùn)算,scores[i]<0 || scores[i]>100;
?count=+1,這是什么操作,沒這樣的寫法,可以count++, ++count, count += 1;
if(count == 3)結(jié)束循環(huán)次數(shù)判斷錯(cuò)誤,第三名不能輸出,應(yīng)該為count > 3。
2019-03-01
for(int i=scores.length-1,count=0;i>=0&&num<3;i--){
if(scores[i]>100||scores[i]<0){
continue;
}else{
count++;
System.out.println(scores[i]);
}
}