简体   繁体   English

在while循环中重复语句Java

[英]Repeating a statement in while loop Java

Sorry for the newbish question, am quite new with Java.抱歉这个新问题,我对 Java 很陌生。

So I want to display an error message when user input is outside of the bounds (Lesser than 0, greater than 100) which I've managed to do but I also want that the user can try again but my current code only continues with the execution of the program.所以我想在用户输入超出我设法做到的界限(小于0,大于100)时显示错误消息,但我也希望用户可以再试一次,但我当前的代码只继续程序的执行。

This is what I have now:这就是我现在所拥有的:

import java.util.Scanner;

public class storeQuota {
   
        public static void main(String [] args) {
        
            Scanner input = new Scanner (System.in);

        
            int quotas [] = new int [100];
            int NumberOfWorkers = 100;

            

           
            for (int i = 0; i<numberOfWorkers; i++) {

                if (i == 0) {
                    System.out.print("Enter the quota for the 1st student: ");
                } 
                else if (i == 1) {
                    System.out.print("Enter the quota for the 2nd student: ");
                } 
                else if (i == 2) {
                    System.out.print("Enter the quota for the 3rd student: ");
                } 
                else if (i >= 3) {
                    System.out.print("Enter the quota for the " + (i+1) + "th student: ");
                }
                     
                while (true) {
                    quotas[i] = input.nextInt();
                        
                    if (quotas[i] > 100 || quotas[i] < 0) 
 
                        System.out.println("Error - Can only be between 0 and 100.");
                        break;
                        
                }
                
            }          
                
            

          //Printing all quotas. 
            System.out.println("Thank you for your input. Your entered quotas are: ");

            for (int i=0; i<numberOfWorkers; i++)   
                    {  
                        System.out.print(quotas[i] + ", ");  
                    }  

            
            
     input.close();
            
        
        }
        
}


With this code, the error message is correctly displayed when a user inputs an int that isn't between 0 and 100 but the user will be unable to try again, the program continues to ask for the next quoata.使用此代码,当用户输入的 int 不在 0 到 100 之间但用户将无法重试时,错误消息会正确显示,程序会继续请求下一个配额。

I think the problem is located in your我认为问题出在您的

break;

after System.out.println("Error - Can only be between 0 and 100.");在 System.out.println("错误 - 只能在 0 到 100 之间。"); which always breaks the while loop.这总是打破while循环。 Instead you only want to break the while loop if the input is in valid range.相反,如果输入在有效范围内,您只想中断 while 循环。 I would not use while(true) but some sort of conditional variable which is set to false in the while loop if the input is in valid range, also because while(true) is not a good programming practice from my point of view.如果输入在有效范围内,我不会使用 while(true),而是在 while 循环中设置为 false 的某种条件变量,这也是因为从我的角度来看,while(true) 不是一个好的编程习惯。

Your problem is using Break;您的问题是使用Break; rather than using that, you should change the while(true) to while(false) , you've also forgot to add curly brackets around the if statement.而不是使用它,您应该将while(true)更改为while(false) ,您还忘记了在 if 语句周围添加花括号。

boolean x = true;
while (x){
quotas[i] = input.nextInt();
if (quotas[i] > 100 || quotas[i] < 0){ 
    System.out.println("Error - Can only be between 0 and 100.");
    x = false;
   }
}

also I suggest learning exceptions as they would make this 10x easier.我还建议学习异常,因为它们会使这 10 倍更容易。

When executed, "break" breaks the loop you are currently in. In your code, break is executed irrespective of what the input is resulting in the unwanted result.执行时,“break”会中断您当前所在的循环。在您的代码中,无论输入导致不需要的结果如何,都会执行 break。

Simplest solution would be (closest to your original code):最简单的解决方案是(最接近您的原始代码):

while(true) {
    quotas[i] = input.nextInt();
    if (quotas[i] > 100 || quotas[i] < 0) {
        System.out.println("Error - Can only be between 0 and 100.");
    } else {
        break;
    }
}

Here, the loop will break only if correct input is entered.在这里,只有输入正确的输入,循环才会中断。

You haven't used curly braces in if condition.您没有在if条件下使用花括号。

while (true) {
 quotas[i] = input.nextInt();
                        
 if (quotas[i] > 100 || quotas[i] < 0) {
 
 System.out.println("Error - Can only be between 0 and 100.");
  break;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM