2 回答

TA貢獻3條經(jīng)驗 獲得超3個贊
你可以先排序,排序是從小到大的,所以你從最后一個開始遍歷,把不符合條件的continue,打印出三個就好了,也就是循環(huán)三次。這樣計較簡單

TA貢獻3條經(jīng)驗 獲得超3個贊
import java.util.Arrays;
public class All {
/*
*1、 考試成績已保存在數(shù)組 scores 中,數(shù)組元素依次為 89 , -23 , 64 , 91 , 119 , 52 , 73
*2、 要求通過自定義方法來實現(xiàn)成績排名并輸出操作,將成績數(shù)組作為參數(shù)傳入
*3、 要求判斷成績的有效性( 0—100 ),如果成績無效,則忽略此成績
*/ ? ?
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? int[] scores={89,-23,64,91,119,52,73};?
? ? All outscores=new All();
? ? outscores.three(scores);
? ? } ??
? ? //定義方法完成成績排序并輸出前三名的功能
public void three(int[] scores){
Arrays.sort(scores);
int count=1;
for(int i=scores.length-1;i>=0;i--){
if(scores[i]<0 || scores[i]>100){
continue;
}
if(count<=3) {
System.out.println(scores[i]);
count++;
}
}
}
}
? ?
? ??
? ??
? ??

TA貢獻17條經(jīng)驗 獲得超7個贊
int?j?=?score.length;?//?數(shù)組下標越界了
參考代碼
import?java.util.Arrays; public?class?HelloWorld?{ ??//?完成?main?方法 ??public?static?void?main(String[]?args)?{ ????int[]?scores?=?{89,?-23,?64,?91,?119,?52,?73}; ????HelloWorld?hello?=?new?HelloWorld(); ????hello.getScores(scores); ??} ??//?定義方法完成成績排序并輸出前三名的功能 ??public?void?getScores(int[]?scores)?{ ????Arrays.sort(scores); ????System.out.println("考試成績的前三名為:"); ????int?count?=?0; ????for?(int?i?=?scores.length?-?1;?i?>=?0;?i--)?{ ??????if?(scores[i]?<?0?||?scores[i]?>?100)?{ ????????continue; ??????}?else?{ ????????while?(count?<?3)?{ ??????????count++; ??????????System.out.println(scores[i]); ??????????break; ????????} ??????} ????} ??} }

TA貢獻2條經(jīng)驗 獲得超2個贊

TA貢獻3593條經(jīng)驗 獲得超0個贊
添加回答
舉報