2017-08-15 21 views
-4

我需要應用將跳過打印前10個值的continue語句。應用將跳過打印程序涉及變量的前10個數值的continue語句

我已經有我的代碼

public class numbers { 

    public static void main(String[] args) { 

     for (int number=1; number <= 99; number++){ 

      if (number % 2 == 0) 
       System.out.print(number + " "); 

         System.out.println(); 
      } 
     } 
    } 
+0

那麼做的,而不是傾銷的東西,也可能只是你有作爲首發爲您的家庭作業模板的「如果」和「繼續」一些研究。提示:你*積極嘗試學習*編程。 – GhostCat

回答

1

只要有另一個櫃檯

int count = 0; 
    for (int number=1; number <= 50; number++){ 

     if (number % 2 == 0 && count++ >= 10) 
     { 
      System.out.print(number + " "); 
      System.out.println(); 
     } 
    } 

,或者如果你想使用一個繼續

int count = 0; 
    for (int number=1; number <= 50; number++){ 

     if (number % 2 == 0) 
     { 
      if (count++ < 10) 
        continue; 

      System.out.print(number + " "); 
      System.out.println(); 
     } 
    } 
0

添加計數器:

public static void main(String[] args) { 
    int counter = 0; 
    for (int number = 1; number <= 50; number++) { 
     if (number % 2 == 0) { 
      counter++; 
      if (counter <= 10) { 
       continue; 
      } 
      System.out.println(number + " "); 
     } 
    } 
} 

與Java 8 Stream的IT可以更文筆優美:

IntStream.rangeClosed(1,50).filter(i -> i % 2 == 0).skip(10).forEach(System.out::println); 
+0

他是一個新手誰要求使用*繼續*,因爲他的任務告訴他這樣做。你的回答並沒有解決所有問題。向一個無法寫下來的人投擲流「繼續」...認真? – GhostCat

+0

那麼,我讀的說*我需要申請一個繼續聲明*。 – GhostCat