2014-02-23 97 views
-1

三個無效條目後,我需要第三次顯示不同的東西並恢復到第一個循環。另外我不知道如何使這個連續如何在一定數量的答案後獲得不同的打印語句

do{ 
    System.out.print("Enter Rating(-1 to quit): "); 
    rating = input.nextInt(); 
    } 
    while (rating == 0 || rating == 1 || rating == 2 || rating == 3 || rating == 4); 
    System.out.print("Invalid entry. Please enter a whole number " 
    + "or enter -1 to quit: "); 
    rating = input.nextInt(); 

回答

0

您可以使用while (rating < 0 || rating > 4)

0

你的意思呢?

/** 
    <P>{@code java Test}</P> 
**/ 
public class Test { 
    public static final void main(String[] ignored) { 
    int iterationCount = 5; 
    for(int i = 0; i < iterationCount; i++) { 
      if(i == 2) { //Index of 3 is 2 
      System.out.println("Printing something special on the third iteration!"); 
      //You could set i back to 0 here, if that's what you want... 
      } else { 
       System.out.println("Index " + i); 
      } 
     } 
    } 
} 

輸出:

[C:\java_code\]java Test 
Index 0 
Index 1 
Printing something special on the third iteration! 
Index 3 
Index 4 
+0

虐待嘗試感謝。看起來像這樣 – user3344218

+0

@ user3344218:如果您發現此答案有幫助,請考慮對其進行投票並接受它,方法是單擊綠色的對號。祝你好運! – aliteralmind