简体   繁体   English

循环执行3次

[英]Loop executing 3 times

I am working on a school exercise that asks me to modify the .java code so that the user is asked to input the right number only 3 times. 我正在做一个学校练习,要求我修改.java代码,以便要求用户输入3次正确的数字。

The code I get is this: 我得到的代码是这样的:

    public static void main (String[] args) {
        Scanner lector = new Scanner(System.in);
        int valorUsuari = 0;
        boolean valorNOk=true;
        while (valorNOk){
            System.out.print("Insert a number between 0 and 5: ");
            valorUsuari = lector.nextInt();
            lector.nextLine();
            if((valorUsuari >= 0)&&(valorUsuari <= 5)){
              valorNOk=false;    
            }
        }
        System.out.println("Data correct. You typed " + valorUsuari);
    }
}

I tried doing this before the while condition and it doesn't seem to work: 我尝试在while条件之前执行此操作,但似乎不起作用:

for (valorUsuari = 0; valorUsuari <= 3 ; valorUsuari++)

Do you have any idea where I am going wrong? 你知道我要去哪里错吗?

Thank you in advance! 先感谢您!

Just add a counter that starts at 0 and when it hits 3, the user is no longer allowed to enter a number. 只需添加一个从0开始的计数器,当计数器达到3时,便不再允许用户输入数字。 Every time the users writes something illegal, add one to the counter. 每次用户写违法的东西时,将其添加到计数器中。

public static void main (String[] args) {
    Scanner lector = new Scanner(System.in);
    int valorUsuari = 0;
    int counter = 0; //this is your counter, it starts at 0 because the user has failed 0 times at this point
    boolean valorNOk=true;
    while (valorNOk && counter < 3){
        System.out.print("Insert a number between 0 and 5: ");
        valorUsuari = lector.nextInt();
        lector.nextLine();
        if((valorUsuari >= 0)&&(valorUsuari <= 5)){
          valorNOk=false;    
        } else {
            counter++; //this is added to keep track of failed tries
        }
    }
    System.out.println("Data correct. You typed " + valorUsuari);
}

Note: This isn't really relevant for you at this point, but your valorNOk is pretty redundant. 注意:这一点与您目前并不实际相关,但是您的valorNOk相当多余。 You can simply use break instead like this: 您可以像这样简单地使用break

public static void main (String[] args) {
    Scanner lector = new Scanner(System.in);
    int valorUsuari = 0;
    int counter = 0; //this is your counter, it starts at 0 because the user has failed 0 times at this point
    while (counter < 3){
        System.out.print("Insert a number between 0 and 5: ");
        valorUsuari = lector.nextInt();
        lector.nextLine();
        if((valorUsuari >= 0)&&(valorUsuari <= 5)){
            break; 
        } else {
            counter++; //this is added to keep track of failed tries
        }
    }
    System.out.println("Data correct. You typed " + valorUsuari);
}

Another note: Even if the user inputs wrongly three times, you will still print "Data correct..." . 另一个注意事项:即使用户输入了3次错误,您仍然会打印"Data correct..." What you could do is something like this after your while-loop: 您可以在while循环后执行以下操作:

if(counter < 3) {
    System.out.println("Data correct. You typed " + valorUsari);
} else {
    System.out.println("Too many tries. You are bad.");
}

Modified it a bit, I used a counter int tries to keep track of the tries. 对其进行了一些修改,我使用了一个计数器int tries来跟踪尝试。

 public static void main(String[] args) throws Exception {
        Scanner lector = new Scanner(System.in);
        int valorUsuari = 0;
        boolean valorNOk = true;
        int tries = 0;
        while (valorNOk && tries < 3) {
            System.out.print("Insert a number between 0 and 5: ");
            valorUsuari = lector.nextInt();
            lector.nextLine();
           if ((valorUsuari >= 0) && (valorUsuari <= 5)) {
            valorNOk = false;
        } else {
            tries++;
        }
        System.out.println("Data correct. You typed " + valorUsuari);
    }

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

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