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

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

為啥輸出的東西不對啊

public static void main(String[] args) {

? ? ? ? int[] scores={89,-23,64,91,119,52,73};

? ? ? ? HelloWorld hello = new HelloWorld();

? ? ? ? hello.youxiao(scores);

? ? ? ? hello.chengji(scores);

? ? }

? ? public void youxiao(int scores[]){

? ? ? ? int i=0;

? ? ? ? for(int score:scores)

? ? ? ? {

? ? ? ? ? ? if(score<100&&score>0)

? ? ? ? ? ? {

? ? ? ? ? ? scores[i]=score;

? ? ? ? ? ? i+=1;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? public void chengji(int scores[]){

? ? ? ? Arrays.sort(scores);? ??

? ? ? ? for(int i=scores.length-3;i<scores.length;i++){

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

? ? ? ? }


? ? }



正在回答

6 回答

輸出了什么東西

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

qq_落定_1 提問者

timeout.我感覺我沒錯啊,是不是方法里對數(shù)組的操作不會同步到主函數(shù)啊
2018-08-02 回復(fù) 有任何疑惑可以回復(fù)我~
#2

Amitie 回復(fù) qq_落定_1 提問者

我把你的代碼運行了一下 輸出 73 89 91輸出是對的只有順序不對,你改一下順序就行了
2018-08-02 回復(fù) 有任何疑惑可以回復(fù)我~
#3

Amitie 回復(fù) qq_落定_1 提問者

你最后循環(huán)輸出用的i++ 本來你用函數(shù)排好序就是{52,64,73,89,91}了 i++當(dāng)然從左邊開始輸出啊,你要從右邊開始輸出,就得用i--。
2018-08-02 回復(fù) 有任何疑惑可以回復(fù)我~
#4

qq_落定_1 提問者 回復(fù) Amitie

for(int i=scores.length-3;i<scores.length;i++){ System.out.println(scores[i]); 所以我這里是直接輸出最后三個啊。直接跳到后面去了的。
2018-08-02 回復(fù) 有任何疑惑可以回復(fù)我~
#5

Amitie 回復(fù) qq_落定_1 提問者

你可以用你的eclipse運行試試 你的代碼我都可以運行成功
2018-08-03 回復(fù) 有任何疑惑可以回復(fù)我~
查看2條回復(fù)

你在main中hello.youxiao(scores);后添加一句System.out.println(Arrays.toString(scores));會發(fā)現(xiàn)scores數(shù)組已經(jīng)變成 [89, 64, 91, 52, 73, 52, 73];然后你再執(zhí)行?hello.chengji(scores)后又會排序,scores數(shù)組變成了,[52, 52, 64, 73, 73, 89, 91];所以你最后輸出的73,89,91。

你可以將youxiao中的改成,如下,將不符合的數(shù)據(jù)改成-1,這樣排序的時候不會影響正常數(shù)值的排序(因為排序是升序,-1都會在數(shù)組最前面),而且也不會改變數(shù)組的長度,所以你最后可以從數(shù)組的最后三個取到正確的值(這樣你就不用判斷數(shù)組的有效值究竟是第幾位到第幾位,只需從最后三個提取數(shù)值就ok)。

for(int?score:scores){
????if(score>100||score<0)
????????scores[i]=-1;
????i++;???//注意i++在if外面
}

這是按照你的思路改的,但是實際上沒有必要寫這么麻煩的。。。你可以再自己考慮更好的思路

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

qq_落定_1 提問者

ok 謝了 剛好我也試了試你說的,發(fā)現(xiàn)了那個數(shù)組后面沒變,忽略了數(shù)組長度沒有變化
2018-08-04 回復(fù) 有任何疑惑可以回復(fù)我~

i<scores.length;?

而且這句不對,i=4了,i又小于數(shù)組長度7,i--。這樣就是死循環(huán)了,因為 i 永遠(yuǎn)小于7

0 回復(fù) 有任何疑惑可以回復(fù)我~
int?temp?=?0;
int?length?=?scores.length;
for(int?i?=?(length-1);?i>=0?&&?temp<3;?i--)?{
????if(scores[i]<0?||?scores[i]>100)
????????continue;
????temp++;
S????ystem.out.println(scores[i]);
}


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

for(int i=scores.length-3;i<scores.length;i++)。這句,數(shù)組長度是7,7-3=4。下表從4開始,i--,依次往下輸出,輸出的元素是下表為[4][3][2][1][0]里的元素了,當(dāng)然不對了。你看看:


int temp = 0;

for(int i = (length-1); i>=0 && temp<3; i--) {

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

continue;

temp++;

System.out.println(scores[i]);

}


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

if(score<100&&score>0)這里不可以是或 要兩者同時滿足


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

Amitie

&&不是或
2018-08-01 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

為啥輸出的東西不對啊

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

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

幫助反饋 APP下載

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

公眾號

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