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

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

以這樣的程序,如何輸出前三名。

public static void main(String[] args) {?

? ? HelloWord hello=new HelloWord();

? ? hello.score();


}

public void score() {

int max=0;

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

for(int i=0;i<score.length;i++){

if(score[i]>=0 && score[i]<=100) {

if(max < score[i]) {

max=score[i];

}

}

}

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

for(int x=0;x<3;x++) {

System.out.println(max);

}

}

}


正在回答

4 回答

還有一種方法 改前面的for循環(huán)

代碼如下:

public void score() {

int max=0;

int[] max=new int[3];? //取前3,因此數(shù)組長(zhǎng)度為3

int count=0;? //該變量代表有效值次數(shù)

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

Arrays.sort(score); // 給數(shù)組score從低到高排序,第一行上邊要有import??java.util.Arrays;

for(int i=0;i<score.length;i++){

if(score[i]>=0 && score[i]<=100) {

if(max < score[i]) {

max=score[i];

}

改為:

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

continue;

}? ? ?//跳過無效值

count++;? //遍歷一個(gè)有效值,有效值次數(shù)加1?

if(count>=3){

break;

}

max[count-1]=score[i];? //此時(shí)count的值為1到3,賦值之后的數(shù)組max中的值從大到小排列

}

}

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

for(int x=0;x<3;x++) {


System.out.println(max);

}

}



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

把max定義為數(shù)組

代碼如下

public void score() {

int max=0; //這個(gè)注釋掉

int[] max=new int[];?

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

for(int i=0;i<score.length;i++){

if(score[i]>=0 && score[i]<=100) {

if(max < score[i]) {

max=score[i];

}

改為:

max[i]=score[i];? ?//這時(shí)數(shù)組max里的值都為有效數(shù)值

}

}

Arrays.sort(max); //給數(shù)組max排序,從低到高

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

for(int x=0;x<3;x++) {


System.out.println(max);

}

改為:

for(int x=max.length-1;x>max.length-4||x>=0;x--){? ?//輸出數(shù)組后三個(gè)數(shù)值,若數(shù)組中的數(shù)值數(shù)量小于3,則輸出全部數(shù)值

????????System.out.println(max[x]);

}

}


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

慕瓜453346

max[i]=score[i]; //這時(shí)數(shù)組max里的值都為有效數(shù)值 這里max里面能為i嗎??
2018-10-10 回復(fù) 有任何疑惑可以回復(fù)我~

你在if條件下給max賦了值,max就會(huì)一直變到循環(huán)結(jié)束,之后成為一個(gè)定值,比如一百以內(nèi)最大值是89,max則在89時(shí)就不會(huì)變化。以至于在下面輸出時(shí)候只能輸出三個(gè)89。在這種前提下只是取出數(shù)組中一百以內(nèi)的最大值,不能得到一百以內(nèi)的前三大的值。

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

按你這個(gè)自己定義的方法是無法輸出前三名的,只能輸出三次同一個(gè)最大值

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

慕粉4333732 提問者

那在這種前提現(xiàn)下,修改代碼能否達(dá)到想要的結(jié)果?請(qǐng)指教
2018-10-03 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

以這樣的程序,如何輸出前三名。

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

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

幫助反饋 APP下載

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

公眾號(hào)

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