import?java.util.Arrays;
public?class?ImoocJava?{
public?static?void?main(String[]?args)?{
int?[]?scores?=?{89,-23,64,91,119,52,73};
System.out.println("考試成績的前三名為:");
int?[]?top?=?getTop3(scores);
System.out.println(Arrays.toString(top));
}
public?static?int?[]?getTop3(int?[]?scores){
Arrays.sort(scores);
int?count?=?0;
int?[]?cj?=?new?int?[3];
for(int?i?=?scores.length-1;i>=0;i--){
if(scores[i]<0||scores[i]>100){
continue;
}
count?++;
for?(int?j?=?0;j<=2;j++){
cj[j]?=?scores[i];
}
if?(count>3){
break;
}
}
return?cj;
}
}
2017-07-09
我認(rèn)為你要加個HelloWorld main=new HelloWorld();
int?[]?top?= main.getTop3(scores);
2017-07-01
額對了,明明不需要返回值到main函數(shù)里面就不要多寫幾行無意義的代碼,直接在方法里輸出就行了。增加可讀性。
2017-07-01
你這就是把簡單問題復(fù)雜化而已 問題在于方法里最后那個if count>3。應(yīng)該把count初始化值改為1或者把那個if搞到最外層(for的下一層)這樣才不會執(zhí)行多一次把第四名打印出來。
你也可以看看我的,思路清晰,實用。寫程序不是只是實現(xiàn)功能就萬事大吉的了還要有維護性可讀性。能不繞圈就盡量簡潔明白。
import java.util.Arrays;
public class HelloWorld?
{//1
public static void main(String[] args)
{//main
int[]scores={89,-23,64,91,119,52,73};
HelloWorld L1=new HelloWorld();
System.out.println("考試成績的前三名為:");
L1.score(scores);
}//main
public void score(int[]shu)
{//方法score ??
int j=0;
Arrays.sort(shu);
for(int i=shu.length-1;i>=0;i--)
{ ??
if(shu[i]>=0&&shu[i]<=100){
j++;
System.out.println(shu[i]);}
if(j==3)
break;
}
}//方法score
}//1
2017-06-29
上面回答 ?有點地方數(shù)據(jù)寫錯了,思路是這樣的。
2017-06-29
首先為什么會輸出3個一樣的呢? 問題在于內(nèi)層for循環(huán)
??for?(int?j?=?0;j<=2;j++){
????????????????cj[j]?=?scores[i];
????????????}
j=0;j<=2;運行一次 cj[j]=scores[i]; 也就是cj[0]=scores[6];j++; j=1;j<=2; cj[i]=scores[6];類推;
為什么輸出會是 64 ?而不是91呢,外層for循環(huán) ,i=6;i>=0;score[i]=91;不滿足if條件,count++;
然后運行 內(nèi)層for循環(huán) ?應(yīng)該是輸出 三個 91 ,但是 count不大于3 ,繼續(xù)運行外層for循環(huán) i--; i=5;i>=0; ?score[5]=89; ?89不滿足if條件 count++;依次內(nèi)推,一直運行到 count>3的時候 跳出。
2017-06-28
import java.util.Arrays;
public class HelloWorld {
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? ? HelloWorld ?hello=new HelloWorld();
? ? ? ? ? ? ? int [] score=new int[]{89,-23,64,91,119,52,73};
? ? ? ? int[] scores=hello.getScores(score);
? ? ? ? Sysotem.out.println("考試成績的前三名為:");
? ? ? ? System.out.println(Arrays.toString(scores));??
? ? }
? ? //定義方法完成成績排序并輸出前三名的功能
? ?public int[] getScores(int[] ?score){
int count=0;
int [] scores=new int[3];
Arrays.sort(score);
for(int i=score.length-1;i>=0;i--){
if(score[i]<0||score[i]>100){
continue;
}else if(count<3){
scores[count]=score[i];
count++;
}
}
return scores;
}
}
2017-06-25
if(scores[i]<0||scores[i]>100){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }else if(count == 3){
? ? ? ? ? ? break;
? ? ? ? ? ? }else{
? ? ? ? ? ? cj[count] = scores[i];
? ? ? ? ? ? count++;
? ? ? ? ? ? }
你for循環(huán)改成這樣試試 ?我這邊剛試了可以了
2017-06-22
2017-06-21
你可以看一下課程的左下角有個 “不會了怎么辦” 圖標(biāo),點開里面有正確答案,一步一步看著他的思路走。
雖然你的整體不對,不過按照你的思路,你的19和20行的語句循環(huán)有錯誤,會一直覆蓋。(仔細(xì)讀你的19和20行的循環(huán)就知道了)
2017-06-21
ImoocJava a=new ImoocJava();然后調(diào)用你的方法