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) 的判斷調換一下位置,這樣就沒有問題了
點擊展開后面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
提交
取消