請大家?guī)臀铱聪履睦镥e了,謝謝?。?/h1>
import java.util.Arrays;
public class HelloWorld {
? ?
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ??
? ? ? ? int []scores={89,-23,64,91,119,52,73};
? ? ? ? ?System.out.println("考試成績的前三名為:");
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? hello.print(scores);
? ? ? ?
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public int print(int []scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ??
? ? ? ? ? ? if((scores[i])>0&&(scores[i])<100){
? ? ? ? ? ? ? for(int j=0;j<3;j++)
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? }
? ? ? ? }
? ? ? ? } ?
}
import java.util.Arrays;
public class HelloWorld {
? ?
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ??
? ? ? ? int []scores={89,-23,64,91,119,52,73};
? ? ? ? ?System.out.println("考試成績的前三名為:");
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? hello.print(scores);
? ? ? ?
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ??
? ? public int print(int []scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;i>=0;i--){
? ? ? ? ??
? ? ? ? ? ? if((scores[i])>0&&(scores[i])<100){
? ? ? ? ? ? ? for(int j=0;j<3;j++)
? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? }
? ? ? ? }
? ? ? ? } ?
}
2016-07-15
錯誤:自定義方法public int print(int []scores)規(guī)定了返回值類型int,但方法體內無return語句進行返回值操作,故報錯
修正:將int改為void
代碼邏輯問題:? 1.if((scores[i])>0&&(scores[i])<100)樓主想用于篩選有效成績,但在此題環(huán)境下較為冗雜,Array.sort()方法是默認升序排列,樓主又使用了(int i=scores.length-1;i>=0;i--)循環(huán)條件,加之預設數組中只有一個負值,所以沒有必要
2.條件執(zhí)行體內的嵌套循環(huán)邏輯錯誤,執(zhí)行結果會變成一個有效成績輸出三次后,進行下一次有效成績判斷。
修成建議:1.有效值判定只保留100臨界判定
2.定義一個計數變量,用于記錄有效值個數以判斷是否終止循環(huán),并刪除嵌套循環(huán)只輸出一次有效值
2016-07-15
方法print沒有返回值類型用void,&&表示與運算,||表示或,有一個成績即可。用計數器進行計算并且輸出指定的那個成績。