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

时间:2017-07-11 14:10:35

标签: java if-statement while-loop calculator break

当我学习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;
                }   
            }
    }       
}

1 个答案:

答案 0 :(得分:0)

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

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

尝试使用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();调用以询问用户输入将始终为空字符串。

相关问题