課程
/后端開發
/Java
/Java入門第一季(IDEA工具)升級版
8-1 break 只跳出一層循環,怎么實現輸出前三名成績
2016-03-01
源自:Java入門第一季(IDEA工具)升級版 7-1
正在回答
public class HelloWorld {import java.util.Arrays;??? //完成 main 方法??? public static void main(String[] args) {??????? HelloWorld getGrades = new HelloWorld(); //創建對象getGrades??? ?int[] result = getGrades.exam(89,-23,64,91,119,52,73);//調用對象并傳入參數??? }??? ??? //定義方法完成成績排序并輸出前三名的功能??? public int[] exam(int a,int b,int c,int d,int e,int f,int g){//構造包含參數的exam方法??? ?int[] scores = {a,b,c,d,e,f,g};//創建數組??Arrays.sort(scores); //對數組進行升序排序??int effGra = 0; //定義并初始化有效成績個數??for (int i = scores.length-1;i >= 0;i--){ //倒序遍歷數組???if ((scores[i]<0)||(scores[i]>100)){ //如果成績小于 0 或大于 100 ????continue;?????? //則使用 continue忽略此成績???}???effGra++; //有效成績+1???if (effGra > 3){ //如果有效成績數大于 3????break; //則結束循環???}???else{ ????System.out.printf(scores[i]+"\t"); //只輸出成績的前三名???}??}??return scores;?}}
我知道為什么了
舉報
0基礎萌新入門第一課,從Java環境搭建、工具使用、基礎語法開始
1 回答循環跳出問題
5 回答如何跳出循環?
1 回答循環輸出問題
1 回答continue跳過循環體是直接跳到最外層循環嗎?
2 回答循環問題???
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-03-05
public class HelloWorld {
import java.util.Arrays;
??? //完成 main 方法
??? public static void main(String[] args) {
??????? HelloWorld getGrades = new HelloWorld(); //創建對象getGrades
??? ?int[] result = getGrades.exam(89,-23,64,91,119,52,73);//調用對象并傳入參數
??? }
???
??? //定義方法完成成績排序并輸出前三名的功能
??? public int[] exam(int a,int b,int c,int d,int e,int f,int g){//構造包含參數的exam方法
??? ?int[] scores = {a,b,c,d,e,f,g};//創建數組
??Arrays.sort(scores); //對數組進行升序排序
??int effGra = 0; //定義并初始化有效成績個數
??for (int i = scores.length-1;i >= 0;i--){ //倒序遍歷數組
???if ((scores[i]<0)||(scores[i]>100)){ //如果成績小于 0 或大于 100
????continue;?????? //則使用 continue忽略此成績
???}
???effGra++; //有效成績+1
???if (effGra > 3){ //如果有效成績數大于 3
????break; //則結束循環
???}
???else{
????System.out.printf(scores[i]+"\t"); //只輸出成績的前三名
???}
??}
??return scores;
?}
}
2016-03-01
我知道為什么了