0

我是一个初学者,我正在尝试在 while 循环中嵌套 if 语句。我用这种方法做了一个基本的计算器,但我有一个小问题。我将首先分享代码,然后提出我的问题。

这是代码:

import java.util.Scanner;
class whileloop{
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
    System.out.println("This is a basic calculator.");
    System.out.println("To exit, input exit when asked to input the method.");
    System.out.println("----------------------------------------------");
    while (true){
        System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
        System.out.println("----------------------------------------------------");
        System.out.print("Please enter a method here:");
        String method = scan.nextLine();
        if (method.equals("exit")){
            System.out.println("You chose to exit.");
            break;
        }
        else if (method.equals("+")){
            System.out.println("You chose to add.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum + snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("-")){
            System.out.println("You chose to subtract.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum - snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("*")){
            System.out.println("You chose to multiply.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum * snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("/")){
            System.out.println("You chose to divide.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum / snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else{
            System.out.println("Invalid input. please select a valid input from the list of operators given.");
        }
    }
}
}

如您所见,它要求一个变量method来处理输入fnumsnum

最后,我添加了 else 语句。当他/她没有输入有效的操作时,它应该通知用户method

问题是,每当我执行此代码时,无论我输入什么,method它都会首先完美运行,询问fnum然后snum输出答案,但随后,它也会执行 else 语句中的代码块。

如何防止这种情况?请帮忙!

此外,这是我得到的那种输出:(我把它放在一个代码块中只是为了让它看起来更有条理。)

to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*'         and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here:+
You chose to add.
Please enter first number here:10
Please enter second number here:10
The answer is20.0
to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*'     and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here:Invalid input. please select a valid input from the list of     operators given.
to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*'   and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here:
4

3 回答 3

2
use scan.nextLine() immediately after scan.nextInt().

每当我们输入一个数字并按 Enter 键时,scan.nextInt() 只会读取数字而不是“行尾”。当我们做 scan.nextLine(); 它从第一个输入读取仍在缓冲区中的“行尾(\n)”。

import java.util.Scanner;
class whileloop{
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
    System.out.println("This is a basic calculator.");
    System.out.println("To exit, input exit when asked to input the method.");
    System.out.println("----------------------------------------------");
    while (true){
        System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
        System.out.println("----------------------------------------------------");
        System.out.print("Please enter a method here:");
        String method = scan.nextLine();
        if (method.equals("exit")){
            System.out.println("You chose to exit.");
            break;
        }
        else if (method.equals("+")){
            System.out.println("You chose to add.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
        scan.nextLine();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
        scan.nextLine();
            double ans = fnum + snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("-")){
            System.out.println("You chose to subtract.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
        scan.nextLine();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
        scan.nextLine();
            double ans = fnum - snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("*")){
            System.out.println("You chose to multiply.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
        scan.nextLine();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
        scan.nextLine();
            double ans = fnum * snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else if(method.equals("/")){
            System.out.println("You chose to divide.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
        scan.nextLine();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
        scan.nextLine();
            double ans = fnum / snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
        else{
            System.out.println("Invalid input. please select a valid input from the list of operators given.");
        }
    }
}
}
于 2012-07-27T10:32:28.827 回答
1

String method = scan.nextLine();计算后返回""else由于将评估任何 if 语句中的空白字符串不匹配

于 2012-07-27T10:33:40.223 回答
0

这是因为计算后scan.nextLine()返回""。所以你可以添加这一行。

     System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
     System.out.println("----------------------------------------------------");
     System.out.print("Please enter a method here:");        
     String method = scan.nextLine();
     // add this line
     if (method.equals(""))
     {
        continue; // no "" will be there 
     }
于 2012-07-27T10:40:02.527 回答