求大神指點代碼錯誤
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;
}
}
2016-10-22
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;
2016-10-22
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個
所以報錯了
2016-10-22
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;
?}
}
剛才給你修改了,現在再給你參考我的,我是錄入成績的方法
2016-10-22
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;
}
}
按照你的代碼修改就是這樣就正確了
2016-10-22
word中的w 要大寫,方法中的for循環中循環條件錯誤,應該是for(int i=0;i<scores.length;i++)