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

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

求大神指點代碼錯誤

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

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

}

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

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) //錯誤

continue;

highestThreeScores [count] = scores[i];?

count++;

}

}

return highestThreeScores;

}

}


正在回答

5 回答

while(count<3) { ? //while語句在這里不適用,放在這里的意思就是要等到里面的for循環語句結束循環后才會跳到count=1這一步,然后再繼續循環for里面的語句,然后重復著上一步得到的數值的語句循環,一直到count=3后才會結束這種循環,但是這樣的話就會重復出現好幾次,總之就是得不到你想要的數值,我建議直接在下面這個for循環語句中設定一個if (count == 3){ break; }條件語句,意識就是說當count=3時就跳出整個循環

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

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

continue;

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

count++;?

}

}

return highestThreeScores; ??

}



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

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 回復 有任何疑惑可以回復我~
#1

Chord4205038 提問者

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

scores[i]<0||scores[i]>100 滿足條件的數據有5個

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

會執行5次

也就是

highestThreeScores [count] = scores[i];?

count++;

會執行5次

int[] highestThreeScores = new int[3];但是定義了highestThreeScores 的長度是3個

所以報錯了

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

Chord4205038 提問者

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

Chord4205038 提問者

原題要求把小于零大于一百的數刨除,所以我執行五次continue只是把不符合要求的數據給排除了,下兩句才是賦值的語句啊,所以我還是只賦了三個值啊,按理來說應該不會出界啊。
2016-10-25 回復 有任何疑惑可以回復我~
#3

Chord4205038 提問者

哦 我看懂了 沒事了
2016-10-25 回復 有任何疑惑可以回復我~

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("你所錄入的成績為:"+Arrays.toString(chengJi));
??System.out.println("排名前三的成績:"+Arrays.toString(san));
??System.out.println("有效成績總數:"+sum);
?}
?
?//接收一個成績數組
?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("請輸入第"+(i+1)+"位學員的成績:");
???stu[i] = shuru.nextInt();
??}
??return stu;
?}
?//返回前三成績
?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;
?}
?
?//返回有效成績總數
?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;
?}
}

剛才給你修改了,現在再給你參考我的,我是錄入成績的方法

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

Chord4205038 提問者

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


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

}

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

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 回復 有任何疑惑可以回復我~
#1

Chord4205038 提問者

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

word中的w 要大寫,方法中的for循環中循環條件錯誤,應該是for(int i=0;i<scores.length;i++)

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

舉報

0/150
提交
取消
Java入門第一季(IDEA工具)升級版
  • 參與學習       1167472    人
  • 解答問題       18748    個

0基礎萌新入門第一課,從Java環境搭建、工具使用、基礎語法開始

進入課程

求大神指點代碼錯誤

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

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

幫助反饋 APP下載

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

公眾號

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