while 循环不断结束而不启动 if 语句

while loop keeps ending without initiating the if-statement

我正在尝试在 Java 上创建一个简单的计算器程序,作为我学习 Java 的第一个简单项目。

问题:在我 运行 程序和计算之后,我想让用户选择他们是想结束程序还是做更多的计算。由于某种原因,程序没有使用我放在其中的 (if) 语句,它似乎跳过它并结束我的程序,不允许用户在最后输入他的选择。

非常抱歉,如果我的问题不清楚,我无法在网上找到问题的解决方案,如果我的代码看起来很乱,我深表歉意。

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double fnum, snum, answer;
        String choice, name, in;

        System.out.println("Hello, whats your name?");
        name = input.nextLine();
        System.out.println("");
        System.out.println("Oh so your name is " + name + "!");
        System.out.println("");
        System.out.println("This program is a calculator that will do simple calculation of two numbers");
        System.out.println("There are four different options to choose from:");

        boolean running = true;

        CAL:
            while (running) {
                System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division");
                System.out.println("Then press enter!");

                choice = input.nextLine();

                while (!choice.equals("a") && 
                       !choice.equals("A") &&
                       !choice.equals("b") &&
                       !choice.equals("B") &&
                       !choice.equals("c") &&
                       !choice.equals("C") &&
                       !choice.equals("d") &&
                       !choice.equals("D")) {
                    System.out.println("Wrong choice, Please try again");
                    System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division");

                    choice = input.nextLine();
                }

                if (choice.equals("a") || choice.equals("A")) {

                    System.out.println(name +" You have chosen Addition");
                    System.out.println("Type the first number:");
                    fnum = input.nextDouble();
                    System.out.println("Type the second number:");
                    snum = input.nextDouble();

                    System.out.println("your answer is:");
                    Addition addy = new Addition(fnum,snum);
                    System.out.println(addy.getans());

                }

                if (choice.equals("b") || choice.equals("B")) {

                    System.out.println(name +" You have chosen Subtraction");
                    System.out.println("Type the first number:");
                    fnum = input.nextDouble();
                    System.out.println("Type the second number:");
                    snum = input.nextDouble();
                    answer = fnum - snum;
                    System.out.println("your answer is:"
                            + answer);

                }

                if (choice.equals("c") || choice.equals("C")) {

                    System.out.println(name +" You have chosen Multiplication");
                    System.out.println("Type the first number:");
                    fnum = input.nextDouble();
                    System.out.println("Type the second number:");
                    snum = input.nextDouble();
                    answer = fnum * snum;
                    System.out.println("your answer is:"
                            + answer);

                }

                if (choice.equals("d") || choice.equals("D")) {

                    System.out.println(name +" You have chosen Addition");
                    System.out.println("Type the first number:");
                    fnum = input.nextDouble();

                    while (fnum == 0) {
                        System.out.println("invalid try again!");
                        fnum = input.nextDouble();
                    }

                    System.out.println("Type the second number:");
                    snum = input.nextDouble();
                    answer = fnum / snum;

                    System.out.println("your answer is:"
                            + answer);              
                }

                System.out.println("");
                System.out.println("Thank you " + name +  " for using this simple calculator :)");

                System.out.println("If you would like to try again press a the press Enter, if you wish to exit press any botton and then press enter");

                in = input.nextLine();

                if (in.equals("a")) {
                    System.out.println("");
                    System.out.println("Thank you, please choose again");
                    System.out.println("");
                } else {
                    System.out.println("Thank you and goodbye");
                    break;
                }   
            }
    }       
}

这是一个由 nextInt/Double/etc 行为引起的简单问题。

这些方法不读取以下换行符,因此下一个 nextLine 将始终 return 空字符串(当前行的其余部分)。

尝试用 Double.parseDouble(input.nextLine());

改变那些(例如加法大小写)
    if (choice.equals("a") || choice.equals("A")) {
        System.out.println(name + " You have chosen Addition");
        System.out.println("Type the first number:");
        fnum = Double.parseDouble(input.nextLine());
        System.out.println("Type the second number:");
        snum = Double.parseDouble(input.nextLine());
        System.out.println("your answer is:");
        Addition addy = new Addition(fnum, snum);
        System.out.println(addy.getans());
    }

您可以注意到调试,最后一个 in = input.nextLine(); 调用以请求用户输入将始终为空字符串。