求大神指點(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;
}
}
2016-10-22
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;
2016-10-22
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ò)了
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("你所錄入的成績(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ī)的方法
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)));
}
//定義方法完成成績(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;
}
}
按照你的代碼修改就是這樣就正確了
2016-10-22
word中的w 要大寫(xiě),方法中的for循環(huán)中循環(huán)條件錯(cuò)誤,應(yīng)該是for(int i=0;i<scores.length;i++)