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

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

求大神指點(diǎn)代碼錯(cuò)誤

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(); //錯(cuò)誤

System.out.println(Arrays.toString(hello.getHighestMarks(scores)));

}

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

public int[] getHighestMarks(int[] scores) {

int count = 0;

int[] highestThreeScores = new int[3];

Arrays.sort(scores);

while(count<3) {

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

if(scores[i]<0||scores[i]>100) //錯(cuò)誤

continue;

highestThreeScores [count] = scores[i];?

count++;

}

}

return highestThreeScores;

}

}


正在回答

5 回答

while(count<3) { ? //while語(yǔ)句在這里不適用,放在這里的意思就是要等到里面的for循環(huán)語(yǔ)句結(jié)束循環(huán)后才會(huì)跳到count=1這一步,然后再繼續(xù)循環(huán)for里面的語(yǔ)句,然后重復(fù)著上一步得到的數(shù)值的語(yǔ)句循環(huán),一直到count=3后才會(huì)結(jié)束這種循環(huán),但是這樣的話(huà)就會(huì)重復(fù)出現(xiàn)好幾次,總之就是得不到你想要的數(shù)值,我建議直接在下面這個(gè)for循環(huán)語(yǔ)句中設(shè)定一個(gè)if (count == 3){ break; }條件語(yǔ)句,意識(shí)就是說(shuō)當(dāng)count=3時(shí)就跳出整個(gè)循環(huán)

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

if(scores[i]<0||scores[i]>100) ? ? ? ? ? ? ? //這里漏了一個(gè)中括號(hào) ?{

continue;

highestThreeScores [count] = scores[i]; ? ? //?這兩個(gè)語(yǔ)句輸出來(lái)的值范圍是0~100之間的,但是你的那個(gè)if條件是大于100小于0的,剛好相反了,所以你應(yīng)該再加一個(gè)else語(yǔ)句,把highestThreeScores [count] = scores[i]; 和?count++;這兩個(gè)語(yǔ)句放到else語(yǔ)句里面來(lái)

count++;?

}

}

return highestThreeScores; ??

}



所以,其它的代碼不需要改,只需要把這一部分應(yīng)該改成:

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

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

????????Three[count]=scores[i];

????????count++;

????????if(count == 3){

????????????break;

????????}

????}

}

return Three;


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

Chord4205038 提問(wèn)者

非常感謝你的詳細(xì)解答~~
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~

scores[i]<0||scores[i]>100 滿(mǎn)足條件的數(shù)據(jù)有5個(gè)

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

會(huì)執(zhí)行5次

也就是

highestThreeScores [count] = scores[i];?

count++;

會(huì)執(zhí)行5次

int[] highestThreeScores = new int[3];但是定義了highestThreeScores 的長(zhǎng)度是3個(gè)

所以報(bào)錯(cuò)了

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

Chord4205038 提問(wèn)者

謝謝你的答案,很受用
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~
#2

Chord4205038 提問(wèn)者

原題要求把小于零大于一百的數(shù)刨除,所以我執(zhí)行五次continue只是把不符合要求的數(shù)據(jù)給排除了,下兩句才是賦值的語(yǔ)句啊,所以我還是只賦了三個(gè)值啊,按理來(lái)說(shuō)應(yīng)該不會(huì)出界啊。
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~
#3

Chord4205038 提問(wèn)者

哦 我看懂了 沒(méi)事了
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~

package comm.word;
import java.util.Arrays;
import java.util.Scanner;

public class Kaoshi {

?/**
? * @param args
? */
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??Kaoshi test = new Kaoshi();
??int[] chengJi = test.jieShou();
??int[] san = test.qianSan(chengJi);
??int sum = test.youXiao(chengJi);
??System.out.println("你所錄入的成績(jī)?yōu)椋?+Arrays.toString(chengJi));
??System.out.println("排名前三的成績(jī):"+Arrays.toString(san));
??System.out.println("有效成績(jī)總數(shù):"+sum);
?}
?
?//接收一個(gè)成績(jī)數(shù)組
?public int[] jieShou(){
??Scanner shuru = new Scanner(System.in);
??int[] stu = new int[8];
??for(int i = 0;i < stu.length;i++){
???System.out.print("請(qǐng)輸入第"+(i+1)+"位學(xué)員的成績(jī):");
???stu[i] = shuru.nextInt();
??}
??return stu;
?}
?//返回前三成績(jī)
?public int[] qianSan(int[] sum){
??Arrays.sort(sum);
??int[] temp = new int[3];
??int j = 0;
??for(int i = sum.length-1; i >= 0;i--){
???temp[j] = sum[i];
???j++;
???if(j == 3)
????break;
??}
??return temp;
?}
?
?//返回有效成績(jī)總數(shù)
?public int youXiao(int[] sum){
??int shu = 0;
??for(int i = 0;i < sum.length;i++){
???if(sum[i] >= 0 && sum[i] <= 100){
????shu++;
???}
??}
??return shu;
?}
}

剛才給你修改了,現(xiàn)在再給你參考我的,我是錄入成績(jī)的方法

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

Chord4205038 提問(wèn)者

非常感謝你能回答的如此詳細(xì)~~
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~


import java.util.Arrays;

public class Asdf {

//完成main方法

public static void main(String[] args) {

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

Helloworld hello = new Helloworld();

System.out.println(Arrays.toString(hello.getHighestMarks(scores)));

}

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

public int[] getHighestMarks(int[] scores) {

int count = 0;

int[] highestThreeScores = new int[3];

Arrays.sort(scores);

for(int i=scores.length-1;i>=0;i--) {
?if(scores[i]<0||scores[i]>100)
??continue;
?if(count<3) {
?highestThreeScores[count] = scores[i];
?count++;
}

}

return highestThreeScores;

}

}


按照你的代碼修改就是這樣就正確了

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

Chord4205038 提問(wèn)者

非常感謝你能回答的如此詳細(xì)~~
2016-10-25 回復(fù) 有任何疑惑可以回復(fù)我~

word中的w 要大寫(xiě),方法中的for循環(huán)中循環(huán)條件錯(cuò)誤,應(yīng)該是for(int i=0;i<scores.length;i++)

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

舉報(bào)

0/150
提交
取消
Java入門(mén)第一季(IDEA工具)升級(jí)版
  • 參與學(xué)習(xí)       1167329    人
  • 解答問(wèn)題       18530    個(gè)

0基礎(chǔ)萌新入門(mén)第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語(yǔ)法開(kāi)始

進(jìn)入課程

求大神指點(diǎn)代碼錯(cuò)誤

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

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

幫助反饋 APP下載

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

公眾號(hào)

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