運行成功,但結果排序和答案不一樣,怎么解決?大神幫
//導入Arrays類
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? HelloWorld hello= new HelloWorld();? ??
? ? int[] scores={89,-23,64,91,119,52,73};? ?
? ? hello.top3(scores);? ??
? ? System.out.println("考試成績前三名:");
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void top3(int[] scores){
? ? //定義一個變量,統計有效成績的前三名的數量
? ? int so=0;
? ??
? ? for ( int i = scores.length - 1; i >= 0; i-- ){
? ? ? ? if(scores[i]>0&&scores[i]>100){
? ? ? ? ? ??
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? so++;
? ? ? ? Arrays.sort(scores);
? ? ? ? System.out.println(scores[i]);
? ? }
? ?
? }
? ??
}
2020-05-01
將數組作為參數進入函數后,開始并未對數組進行處理,for語句取數組的最后一個元素為73,if語句不滿足,不執行continue,循環繼續,so++,數組排序,輸出排序后最后一個值,即最大值119,進入第二次循環,輸出倒數第二大的值91,后續一直未滿足if語句,持續輸出,直到遍歷完整個數組。
2020-04-30
? Arrays.sort(scores);排序應該放在for循環前面,而且你的判斷語句也錯了。
2020-04-23
你的判斷語句出錯了,應該是scores[i]<=100
2020-04-05
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.sort(scores);
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public int[] sort(int[] scores){
? ? ? ? Arrays.sort(scores);
? ? ? ? int count = 0;
? ? ? ? for(int i =scores.length-1;i>=0;i--){
? ? ? ? ? ? if(scores[i]<=100&&scores[i]>=0){
? ? ? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? ? ??
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? if(count==3){
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return scores;