java中的简单计算器 - 使用布尔值询问用户是否要继续

时间:2016-02-08 05:12:11

标签: java while-loop boolean calculator

我正在使用JOptionPane在java中编写基本的计算器程序。我对此非常陌生,到目前为止我得到的所有帮助都非常感谢!

我的程序假设要求用户输入一个号码,而不是让他们选择要使用的运算符(+, - ,*,/,%),而不是要求用户输入第二个号码。完成所有这些后,将执行计算并显示结果。

我有工作代码循环计算器,以便在计算完成后,系统会询问用户是否要再次使用它。我的代码有效,但我想调整的是,当用户被问及是否要继续并且他们说是,而不是循环返回并让输入框说“欢迎来到计算器!\ n请输入您的第一个号码:“)我希望它只能说”请输入您的第一个号码:“而不是继续(因为用户不需要再次欢迎)。

public static void main(String[] args) {
    double numOne; //first number as Float
    double numTwo; //second number as Float
    double result; //result of equation
    String num1; //first number as String
    String num2; //second number as String
    String input; //operator type (+,-,*,/,%)
    boolean useCalculator = false; //False to quit, True to use

    while (!useCalculator) {
        num1 = JOptionPane.showInputDialog(null, "Welcome to the calculator!\nPlease enter your first number:");
        numOne = Double.parseDouble(num1);

        input = JOptionPane.showInputDialog(null, "Thank you! What would you like to do?\nPlease choose from the following options:\n+\n-\n*\n%\n/");
        while (input == null || !(input.equals("+") || input.equals("*") || input.equals("/") || input.equals("-"))) {
            input = JOptionPane.showInputDialog(null, "The operator is invalid. Please select a correct operator.\nPlease choose from the following options:\n+\n-\n*\n%\n/");
        }
        num2 = JOptionPane.showInputDialog(null, "Thank you!\nPlease enter your second number:");
        numTwo = Double.parseDouble(num2);
    }
}

public static boolean askYesNoQuestion(String prompt) {
    String confirmationInput;
    boolean repeat = false; //false to quite calc, true to repeat use of calc

    while (true) {
        confirmationInput = JOptionPane.showInputDialog(null,
                prompt + " (Please answer yes or no)");

        if (confirmationInput.equalsIgnoreCase("y") || confirmationInput.equalsIgnoreCase("yes")) {
            prompt = "Please enter your first number";
            return true;
        } else if (confirmationInput.equalsIgnoreCase("n") || confirmationInput.equalsIgnoreCase("no")) {
            JOptionPane.showMessageDialog(null, "You have decided to quit the calculator program.\n Goodbye!");
            return false;
        }

        if (!repeat) {
            prompt = "That is an invalid choice. " + prompt;
            repeat = true;
        }
    }
}

与我的其他问题相同,我想了解并学习如何做到这一点。为了清晰起见,我添加了部分代码(如果需要,我可以添加整个代码)但我真正想要的是如何实现这一点的解释,以便我可以自己尝试并通过它来完成。如果这是广泛的,我道歉,如果需要,我很乐意清除任何事情! :)

@ajb 大家好 - 我已经尝试删除一些更改(添加另一个while循环并删除提示)但我收到了返回错误。我试图查看这个,我发现它与使用void的main方法有关,但我不确定如何解决这个问题。如果有人对下面的新代码有任何进一步的提示(所以我可以再次尝试并完成它),我真的很感激!

import javax.swing.JOptionPane;

public class test {

    public static void main(String[] args) {
        double numOne; //first number as Float
        double numTwo; //second number as Float
        double result; //result of equation
        String num1; //first number as String
        String num2; //second number as String
        String input; //operator type (+,-,*,/,%)
        String confirmationInput;
        boolean useAgain = false;
        boolean useCalculator = false; //false if user decides to quit, true if user continues to use calculator


        while (!useCalculator) {
            num1 = JOptionPane.showInputDialog(null, "Welcome to the calculator!\nPlease enter your first number:");
            numOne = Double.parseDouble(num1);

            input = JOptionPane.showInputDialog(null, "Thank you! What would you like to do?\nPlease choose from the following options:\n+\n-\n*\n%\n/");

            //while loop gathers user input. Asks user repeatedly until a valid operator is entered
            while (input == null || !(input.equals("+") || input.equals("*") || input.equals("/") || input.equals("-") || input.equals("%"))) {
                input = JOptionPane.showInputDialog(null, "The operator " + input + "  is invalid. Please select a correct operator.\nPlease choose from the following options:\n+\n-\n*\n%\n/");
            }
            num2 = JOptionPane.showInputDialog(null, "Thank you!\nPlease enter your second number:");
            numTwo = Double.parseDouble(num2);

            //calculates result of user input based on operator choosen
            if (input != null && input.equals("+")) {
                add(numOne, numTwo);
                result = add(numOne, numTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numOne + " " + input + " " + numTwo + " is: " + result);
            } else if (input != null && input.equals("-")) {
                minus(numOne, numTwo);
                result = minus(numOne, numTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numOne + " " + input + " " + numTwo + " is: " + result);
            } else if (input != null && input.equals("*")) {
                multiply(numOne, numTwo);
                result = multiply(numOne, numTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numOne + " " + input + " " + numTwo + " is: " + result);
            } else if (input != null && input.equals("/")) {
                divide(numOne, numTwo);
                result = divide(numOne, numTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numOne + " " + input + " " + numTwo + " is: " + result);
            } else if (input != null && input.equals("%")) {
                modul(numOne, numTwo);
                result = modul(numOne, numTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numOne + " " + input + " " + numTwo + " is: " + result);
            }

            if (!useCalculator) {
              JOptionPane.showInputDialog(null, "Would you like to continue using this calculator? (Please answer yes or no)");
while (true) {

            if (confirmationInput.equalsIgnoreCase("y") || confirmationInput.equalsIgnoreCase("yes")) {
                num1 = JOptionPane.showInputDialog(null, "Please enter your first number:");
                return true;
            } else if (confirmationInput.equalsIgnoreCase("n") || confirmationInput.equalsIgnoreCase("no")) {
                JOptionPane.showMessageDialog(null, "You have decided to quit the calculator program.\n Goodbye!");
                return false;
            }

            if (!useAgain) {
                JOptionPane.showInputDialog(null, "That is an invalid choice.\nWould you like to continue using this calculator? (Please answer yes or no)");

                useAgain = true;
            }
}
            }


        System.out.println("End of calculator program. Program completed successfully");
    }
    }

0 个答案:

没有答案