斷橋丶曉風(fēng)殘月
2016-06-29 17:04:50
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++;
???????????? }
??????? }
????? }
}
代碼要實現(xiàn)輸出考試成績的前三名。
}
3 回答
已采納

綠洲仙人球
TA貢獻(xiàn)39條經(jīng)驗 獲得超47個贊
循環(huán)怎么嵌套應(yīng)該和需求有關(guān)系,我讀了一下題主的代碼,就貼出來的這部分而言是無法運(yùn)行的,一是代碼語法有點(diǎn)錯誤,而是根據(jù)需求,要輸出考試成績的前三名,代碼的邏輯也有點(diǎn)問題,在沒有大改動題主的代碼的基礎(chǔ)上,實現(xiàn)了功能,代碼如下
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循環(huán)和if?(count?<?3) 的判斷調(diào)換一下位置,這樣就沒有問題了
點(diǎn)擊展開后面3條

qq_匡璐_0
TA貢獻(xiàn)96條經(jīng)驗 獲得超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
提交
取消