简体   繁体   English

java 带有 if/else 语句的 while 循环,else 参数永远运行而不是重新启动 while 循环

[英]java while loop with if/else statement, else argument runs forever instead of restarting while loop

So I have an assignment where I am supposed to prompt the user to enter an arithmetic operator, and then 2 numbers, and produce the result;所以我有一个任务,我应该提示用户输入算术运算符,然后输入 2 个数字,并产生结果; ie user enters /, 20, 2 and the OUTPUT should be 10. I am using checks to ensure the proper value types are entered by the user, and my check for the integers entered runs forever if the else statement is activated.即用户输入/、20、2,OUTPUT 应该是10。我正在使用检查来确保用户输入了正确的值类型,如果激活了else 语句,我对输入的整数的检查将永远运行。 I am not sure where I am going wrong here.我不确定我在哪里出错了。 any guidance would be greatly appreciated.任何指导将不胜感激。

import java.util.Scanner;导入 java.util.Scanner; public class mainClass {公共class mainClass {

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    boolean charCheck = true; //for verifying user arithmetic input
    boolean numCheck = true; //for verifying user int input
    boolean sysCheck = true; //for program outer while loop
    int n1 = -1; //user entered number 1
    int n2 = -2; //user entered number 2
    char x = ' '; //user entered arithmetic operator
    while (sysCheck == true)
    {
    while (charCheck == true)
    {
        System.out.println("Please enter either +, -, *, /to proceed. Enter x to end the program");
        x = input.next().charAt(0);

        switch(x) //check input validity for x before the program proceeds
        {
            case '+':
            case '-':
            case '*':
            case '/':    
              System.out.println(x);
              charCheck = false;
              break;
            case 'x':
                System.exit(0);
                break;
            default:
                System.out.print("That is an invalid entry. ");
        }
    }
        System.out.println("Now, please enter 2 numbers, seperated by a space: ");
        while(numCheck)
        {
        if(input.hasNextInt())
        {
        n1 = input.nextInt();
        n2 = input.nextInt();
        System.out.println(n1 + " " + n2);
        numCheck = false;
        }
        else
            System.out.print("That is an invalid entry. ");                
        }
        
        switch(x) //check input validity for x before the program proceeds
        {
            case '+':
                System.out.print("The result is: " + (n1 + n2));
                break;
            case '-':
                System.out.print("The result is: " + (n1 - n2));
                break;
            case '*':
                System.out.print("The result is: " + (n1 * n2));
                break;                   
            case '/':
                System.out.print("The result is: " + (n1 / n2));
                break;
        }


    }
}
}

After playing around with the code more and researching more, I found the solution.在更多地使用代码并进行更多研究之后,我找到了解决方案。 the issue I was having was due to not including the line input.next();我遇到的问题是由于不包括行 input.next(); in my else statement (input is the name of my scanner, if you named your scanner something else, that name would be in it's place)在我的 else 语句中(输入是我的扫描仪的名称,如果您将扫描仪命名为其他名称,则该名称将在它的位置)

        while(numCheck)
        {
        System.out.println("Now, please enter 2 numbers, seperated by a space: ");
        
        if(!input.hasNextInt())
        {
        System.out.println("That is an invalid entry. ");       
        input.next();
        }
        else
        {
        n1 = input.nextInt();
        n2 = input.nextInt();
        numCheck = false;
        }            
        }
        

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

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