為什么用continue的多,而我用的break
為什么用continue的多,而我用的break,感覺此情景下break合適一點
import java.util.Arrays;
public class HelloWorld {
? ? //完成 main 方法
? ? public static void main(String[] args) {
? ? ? ? HelloWorld hello=new HelloWorld();
? ? ? ? int[] sorces= {89,-23,64,91,119,52,73};
? ? ? ? hello.sorts(sorces);
? ? }
? ? //定義方法完成成績排序并輸出前三名的功能
? ? public void sorts(int[] sorces) {
? ? int count=0;
? ? Arrays.sort(sorces);
? ? System.out.println("考試成績前三名為:");
? ? //System.out.println(Arrays.toString(scores));
? ? for(int i=sorces.length-1;i>=0;i--) {
? ? if(sorces[i]>0&&sorces[i]<=100) {
? ? System.out.println(sorces[i]);
? ? ? ? count++;
? ? }
? ? if(count==3) break;? ?
? ? }
? ?}
2019-06-20
continue 是結束本次循環跳入下一次循環,循環不結束;break是跳出當前循環,循環結束。在你的if判斷中是輸入前三名就結束循環,不然繼續遍歷數組輸出。寫程序每個人思路不同,自然用的結束語句也不同,程序沒毛病。
2019-05-18
個人理解啊,不知道對不對
1、你的代碼和參考的答案的循環條件不一樣
參考答案的循環條件是(小于0或者大于100)
達不到條件時:跳出(用continue)
達到對應條件:輸出
輸出次數為3時停止循環。
你的答案的循環條件是(大于0且小于等于100)(你循環條件少一個等于號)
達不到條件時:繼續循環
達到條件時:輸出
輸出次數為3時停止循環。
所以你的方法不需要continue來跳出循環。
p.s.你代碼的最后一個花括號沒粘上。。。。。