課程
/后端開發(fā)
/Java
/Java入門第一季(IDEA工具)升級版
有同學(xué)考慮過第三位有兩個分數(shù)相同的情況嗎?這種情況該怎么寫呢?
2017-05-27
源自:Java入門第一季(IDEA工具)升級版 7-1
正在回答
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args){
int scores[]={89,-23,73,91,119,52,73};
HelloWorld hello=new HelloWorld();
hello.chenJi(scores);
}
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void chenJi(int [] cores){
int a=0;
int b=0;
Arrays.sort(cores);
System.out.println("考試成績的前三名為:");
for(int i=cores.length-1;i>=0;i--){//排除大于100和小于0的數(shù).
if(cores[i]>100||cores[i]<0){
continue;
}else if((b<3)||(cores[i+1]==cores[i])){//判斷是否滿足前三名,如果第三名有兩個相同的成績,就把相同的成績也打印出來.如果沒有相同成績就只打印3個成績.
a=cores[i];
b++;
System.out.println(a);
}else{
break;
? ?
蒼蠅拍拍 提問者
public?class?helloWorld?{ public?static?void?main(String[]?args)?{ //?定義一個整型數(shù)組,保存成績信息 int[]?scores?=?{?89,?89,?64,?91,?73,?89,?73}; helloWorld?hello?=?new?helloWorld(); hello.thirdCore(scores); } public?void?thirdCore(int[]?scores){ Arrays.sort(scores); int?count?=?1; for(int?i=(scores.length-1);i>=0;i--){ if(scores[i]<0?||?scores[i]>100){ continue; }else{ if(scores[i]?==?scores[i-1]){ System.out.print("索引為"+i+":"+scores[i]); }else{ count++; System.out.println("索引為"+i+":"+scores[i]); } if(count>3){ break; } } } } }
舉報
0基礎(chǔ)萌新入門第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語法開始
4 回答第三章的問題
1 回答怎么證明999是個三位數(shù)
2 回答為什么第三個選項不對
1 回答第三個數(shù)為什么不顯示
2 回答第三行不懂
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-05-28
import java.util.Arrays;
public class HelloWorld {
? ??
? ? //完成 main 方法
? ? public static void main(String[] args){
int scores[]={89,-23,73,91,119,52,73};
HelloWorld hello=new HelloWorld();
hello.chenJi(scores);
}
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void chenJi(int [] cores){
int a=0;
int b=0;
Arrays.sort(cores);
System.out.println("考試成績的前三名為:");
for(int i=cores.length-1;i>=0;i--){//排除大于100和小于0的數(shù).
if(cores[i]>100||cores[i]<0){
continue;
}else if((b<3)||(cores[i+1]==cores[i])){//判斷是否滿足前三名,如果第三名有兩個相同的成績,就把相同的成績也打印出來.如果沒有相同成績就只打印3個成績.
a=cores[i];
b++;
System.out.println(a);
}else{
break;
}
}
}
? ?
2017-05-27