如何在一定数量的答案后获得不同的打印语句

  • 本文关键字:语句 打印 答案 java
  • 更新时间 :
  • 英文 :


在三个无效条目之后,我需要第三次显示不同的东西并恢复到第一个循环。也不确定如何使这个连续

    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();
您可以使用

while (rating < 0 || rating > 4)

你是这个意思吗?

/**
   <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

最新更新