課程
/后端開發(fā)
/Java
/Java入門第一季(IDEA工具)升級版
有效成績那么多
2018-07-13
源自:Java入門第一季(IDEA工具)升級版 7-1
正在回答
先把數(shù)組按從小到大排序好,然后從后往前取三個(有效?。。。┑某煽?/p>
把有效數(shù)據(jù)排序,從最大的數(shù)往下取三位就好。
注意任務(wù)一的第四、五條,增加判斷成績是否有效的條件,定義一個變量用來記錄當(dāng)前有效成績的個數(shù)。
import java.util.Arrays;
public class HelloWorld {
? ? ?//完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[] scores=new int[]{89 , -23 , 64 , 91 , 119 , 52 , 73} ;
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? hello.resultsSort(scores);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void resultsSort(int[] scores){
? ? ? ? int count=0;
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;(i>=0&&count<3);i--){
? ? ? ? ? ? if(!(scores[i]<0||scores[i]>100)){
? ? ? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? }else
? ? ? ? ? ? continue;
? ? ? ? }
}
對成績從小到大排序,然后倒序輸出,規(guī)定一個變量判斷是否有效成績,如果這個成績有效,則變量加一,直到變量等于3退出循環(huán)。
public static void main(String[] args) {??int []scores={89,-23,64,91,119,52,73};??Arrays.sort(scores);??int count=1; //標(biāo)記有效成績??System.out.println("成績前三名:");??output(scores,count);?}?public static void output(int []s,int c) {??for(int i=s.length-1;i>=0;i--) {???if (s[i]>=0&&s[i]<=100&&c<=3) {????c++;????System.out.print(s[i]+" ");???}??????}?}
可參考我的代碼
舉報
0基礎(chǔ)萌新入門第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語法開始
2 回答輸出前三名成績
1 回答為什么不輸出成績,只輸出上面的考試成績前三名。
2 回答這個代碼怎么改就可以輸出前3名成績,謝謝
2 回答前三名不都是有效成績嗎,為什么還要判斷是否為有效成績???
1 回答為什么有這個最后就會是前三名的成績輸出
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2018-07-30
先把數(shù)組按從小到大排序好,然后從后往前取三個(有效?。。。┑某煽?/p>
2018-07-23
把有效數(shù)據(jù)排序,從最大的數(shù)往下取三位就好。
2018-07-14
注意任務(wù)一的第四、五條,增加判斷成績是否有效的條件,定義一個變量用來記錄當(dāng)前有效成績的個數(shù)。
import java.util.Arrays;
public class HelloWorld {
? ? ?//完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? int[] scores=new int[]{89 , -23 , 64 , 91 , 119 , 52 , 73} ;
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? System.out.println("考試成績的前三名為:");
? ? ? ? hello.resultsSort(scores);
? ? ? ??
? ? }
? ??
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void resultsSort(int[] scores){
? ? ? ? int count=0;
? ? ? ? Arrays.sort(scores);
? ? ? ? for(int i=scores.length-1;(i>=0&&count<3);i--){
? ? ? ? ? ? if(!(scores[i]<0||scores[i]>100)){
? ? ? ? ? ? ? ? System.out.println(scores[i]);
? ? ? ? ? ? ? ? count++;
? ? ? ? ? ? }else
? ? ? ? ? ? continue;
? ? ? ? }
? ? }
? ??
? ??
? ??
? ??
? ??
? ??
? ??
? ??
? ??
}
2018-07-13
對成績從小到大排序,然后倒序輸出,規(guī)定一個變量判斷是否有效成績,如果這個成績有效,則變量加一,直到變量等于3退出循環(huán)。
public static void main(String[] args) {
??int []scores={89,-23,64,91,119,52,73};
??Arrays.sort(scores);
??int count=1; //標(biāo)記有效成績
??System.out.println("成績前三名:");
??output(scores,count);
?}
?public static void output(int []s,int c) {
??for(int i=s.length-1;i>=0;i--) {
???if (s[i]>=0&&s[i]<=100&&c<=3) {
????c++;
????System.out.print(s[i]+" ");
???}
????
??}
?}
可參考我的代碼