亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

代碼通過了,但是運行結果不對,求大神指點迷津

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.scoresSort(scores,scores.length);

? ? }

? ??

? ? //定義方法完成成績排序并輸出前三名的功能

? ? public void scoresSort(int[] scores,int num){

? ? ? ? int[] scoresCorrect=new int[num];

? ? ? ? Arrays.sort(scores);

? ? ? ? for(int i=scores.length-1,j=0;i>=0;j++){

? ? ? ? ? ? if(scores[i]<0 && scores[i]>100){

? ? ? ? ? ? ? ? i--;

? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? scoresCorrect[j]=scores[i];

? ? ? ? ? ? ? ? i--;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? System.out.println("考試成績的前三名為:");

? ? ? ? for(int i=scoresCorrect.length-1;i>=scoresCorrect.length-3;i--){

? ? ? ? ? ? System.out.println(scoresCorrect[i]);

? ? ? ? }

? ? }

? ??

https://img1.sycdn.imooc.com//5c64d3890001a15004880184.jpg?這是我的錯誤的運行結果

https://img1.sycdn.imooc.com//5c64d38a0001b2cc04610181.jpg這是正確的結果


正在回答

3 回答

別說,你這bug挺多的。

1)score<0||score>100,是或不是且

2)for循環里第三項j++刪掉,把j++加進else里;或者把j++改成i--,把句里的if和else里的i--全刪掉,再把j++加進else

3)你是先把原數組從小到大排序之后,再從尾到頭有選擇的放進correct數組里,所以correct數組本就已經是從大到小的順序,你輸出的時候應該從前向后輸出,不是從后向前輸出。

發一個給你debug之后的版本:

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.scoresSort(scores,scores.length);

? ? }

? ??

? ? //定義方法完成成績排序并輸出前三名的功能

? ? public void scoresSort(int[] scores,int num){

? ? ? ? int[] scoresCorrect=new int[num];

? ? ? ? Arrays.sort(scores);

? ? ? ? for(int i=scores.length-1,j=0;i>=0;){

? ? ? ? ? ? if(scores[i]<0 || scores[i]>100){

? ? ? ? ? ? ? ? i--;

? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? scoresCorrect[j]=scores[i];

? ? ? ? ? ? ? ? i--;

? ? ? ? ? ? ? ? j++;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? System.out.println("考試成績的前三名為:");

? ? ? ? for(int i=0;i<3;i++){

? ? ? ? ? ? System.out.println(scoresCorrect[i]);

? ? ? ? }

? ? }

}

另外你寫這個好麻煩啊,直接輸出就行沒必要再列到數組輸出;數組長度本就可以.length得到沒必要再設一個參數;此外排序之后后面的數只可能大于100不可能小于零,不用判斷是否小于零;最后,你只需要前三個,沒必要把所有有效數字都排一遍。

我發一下我寫的吧。

public void prints(int[] score){

? ? Arrays.sort(score);

? ? System.out.println("考試成績的前三名為:");

? ? int i=score.length-1;

? ? for(int j=1;j<=3;i--){

? ? if(score[i]>100)

? ? continue;

? ? else

? ? ? ? {System.out.println(score[i]);j++;}

? ? }


0 回復 有任何疑惑可以回復我~

?public static void main(String[] args) {
???????? int[] scores={89,-23,64,91,119,52,73};
???????? HelloWorld hello=new HelloWorld();
???????? hello.scoresSort(scores,scores.length-2);
???? }
????
???? //定義方法完成成績排序并輸出前三名的功能
???? public void scoresSort(int[] scores,int num){
???? ? int[] scoresCorrect=new int[num];
???????? Arrays.sort(scores);
???????? int i=0,j=0;
?????? for(i=0;i<scores.length;i++){
???????????? if(scores[i]>0 && scores[i]<100){
???????????? ?scoresCorrect[j] = scores[i];
???????????? ?j++;?
???????????? }
??????? }?System.out.println("輸出前三名成績:");
???????????? for(j=scoresCorrect.length-1;j>=scoresCorrect.length-3;j--){
???? ???? System.out.println(scoresCorrect[j]);
???????? }
??????? }

0 回復 有任何疑惑可以回復我~

public static void main(String[] args) {
???????? int[] scores={89,-23,64,91,119,52,73};
???????? HelloWorld hello=new HelloWorld();
???????? hello.scoresSort(scores,scores.length-2);
???? }
????
???? //定義方法完成成績排序并輸出前三名的功能
???? public void scoresSort(int[] scores,int num){
???? ? int[] scoresCorrect=new int[num];
???????? Arrays.sort(scores);
???????? int i=0,j=0;
??????? while(i<scores.length){
???????????? if(scores[i]>0 && scores[i]<100){
???????????? ?scoresCorrect[j] = scores[i];
???????????? ?j++;
???????????? ?i++;?
???????????? }else{
???????????? ?i++;
???????????? }??
??????? }?System.out.println("輸出前三名成績:");
???????????? for(j=scoresCorrect.length-1;j>=scoresCorrect.length-3;j--){
???? ???? System.out.println(scoresCorrect[j]);
???????? }

???????
???????? }

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

代碼通過了,但是運行結果不對,求大神指點迷津

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號