第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

代碼通過(guò)了,但是運(yùn)行結(jié)果不對(duì),求大神指點(diǎn)迷津

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);

? ? }

? ??

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

? ? 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("考試成績(jī)的前三名為:");

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

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

? ? ? ? }

? ? }

? ??

https://img1.sycdn.imooc.com//5c64d3890001a15004880184.jpg?這是我的錯(cuò)誤的運(yùn)行結(jié)果

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


正在回答

3 回答

別說(shuō),你這bug挺多的。

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

2)for循環(huán)里第三項(xiàng)j++刪掉,把j++加進(jìn)else里;或者把j++改成i--,把句里的if和else里的i--全刪掉,再把j++加進(jìn)else

3)你是先把原數(shù)組從小到大排序之后,再?gòu)奈驳筋^有選擇的放進(jìn)correct數(shù)組里,所以correct數(shù)組本就已經(jīng)是從大到小的順序,你輸出的時(shí)候應(yīng)該從前向后輸出,不是從后向前輸出。

發(fā)一個(gè)給你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);

? ? }

? ??

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

? ? 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("考試成績(jī)的前三名為:");

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

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

? ? ? ? }

? ? }

}

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

我發(fā)一下我寫的吧。

public void prints(int[] score){

? ? Arrays.sort(score);

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

? ? 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 回復(fù) 有任何疑惑可以回復(fù)我~

?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);
???? }
????
???? //定義方法完成成績(jī)排序并輸出前三名的功能
???? 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("輸出前三名成績(jī):");
???????????? for(j=scoresCorrect.length-1;j>=scoresCorrect.length-3;j--){
???? ???? System.out.println(scoresCorrect[j]);
???????? }
??????? }

0 回復(fù) 有任何疑惑可以回復(fù)我~

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);
???? }
????
???? //定義方法完成成績(jī)排序并輸出前三名的功能
???? 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("輸出前三名成績(jī):");
???????????? for(j=scoresCorrect.length-1;j>=scoresCorrect.length-3;j--){
???? ???? System.out.println(scoresCorrect[j]);
???????? }

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

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

代碼通過(guò)了,但是運(yùn)行結(jié)果不對(duì),求大神指點(diǎn)迷津

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)