【问题标题】:while loop keeps ending without initiating the if-statementwhile 循环不断结束而不启动 if 语句
【发布时间】:2017-12-15 15:49:05
【问题描述】:

我正在尝试在 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;
                }   
            }
    }       
}

【问题讨论】:

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


    【解决方案1】:

    这是一个由 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(); 要求用户输入将始终为空字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 2022-09-29
      • 2013-06-09
      相关资源
      最近更新 更多