如何修复我的Java代码,以便在用户输入" quit"?时结束

时间:2014-10-22 01:34:11

标签: java loops calculator quit

所以我已经在Java中使用这个简单的计算器了一段时间了,我希望程序在用户键入" quit"在扫描仪中。我尝试将此功能实现为“如果'在我的主要课程结尾附近的声明:

if (input.equals("quit")) {
                System.out.println("Thanks for using my program.");
                System.exit(0);
            } 

我的IDE无法识别任何错误,但是当我编译并运行我的程序时,并尝试通过键入' quit'来退出程序。进入扫描仪,程序结束(有错误)而不显示我的退出消息。

我的代码如下:

 package calculator;
    import java.util.Scanner;

    public class Calculator {

    public static void main(String[] args) {

        System.out.println("Hello, welcome to my calculator");
        System.out.println("Enter in some stuff you want to me to calculate");

        Scanner scan = new Scanner(System.in);
        System.out.println("If you need help please type \"help\"");
        System.out.println("If at anytime you want to leave, type \"quit\"");
        System.out.println("If you want to continue, hit enter.");

        String s1 = scan.nextLine();

        if (s1.equals("help")){
            System.out.println(" ");
            System.out.println("Double operand commands:");
            System.out.println("Addition: '+' (Ex: 'a + b' )");
            System.out.println("Subtraction: '-' (Ex: 'a - b' )");
            System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
            System.out.println("Division: '/' (Ex: 'a / b' )");
            System.out.println("Exponents: '^' (Ex: 'a ^ b' )");
            System.out.println(" ");
        }

        Scanner input = new Scanner(System.in);

        Maths maths = new Maths();

        double answer = 0;
        double numA, numB;
        char operator;
        boolean quit = false;

         while (quit == false) {
            System.out.print("Please enter your equation: ");

            numA = input.nextDouble();
            operator = input.next().charAt(0);
            numB = input.nextDouble();        

            if (operator == '+') {
                answer = maths.add(numA, numB);
            }

            if (operator == '-') {
                answer = maths.subtract(numA, numB);
            }

            if (operator == '*') {
                answer = maths.multiply(numA, numB);
            }

            if (operator == '/') {
                answer = maths.divide(numA, numB);
            }

            if (operator == '^') {
                answer = maths.power(numA, numB);
            }

            System.out.println(answer);        

        if (input.equals("quit")) {
            System.out.println("Thanks for using my program.");
            System.exit(0);
        }

         }       

        input.close();

        }

    }

    class Maths {

        double add(double a, double b) {
            double answer = a+b;
            return answer;          
        }

        double subtract(double a, double b) {
            double answer = a-b;
            return answer;          
        }

        double multiply(double a, double b) {
            double answer = a*b;
            return answer;          
        }

        double divide(double a, double b) {
            double answer = a/b;
            return answer;          
        }

        double power(double a, double b){
            double answer =a;

            for (int x=2; x<=b; x++){
                answer *= a;
            }

            return answer;
        }

    }

如果你能告诉我什么是错的,我会很感激,这样我的代码最终会有效,所以我将来不会犯同样的错误!

提前致谢

5 个答案:

答案 0 :(得分:0)

当用户键入quit时尝试在if语句中将boolean quit设置为true,这样它就会退出while循环。

答案 1 :(得分:0)

您正在获取InputMismatchException,因为您在预期双倍时为扫描程序提供字符串。

应使用scanner.nextLine()方法读取字符串变量。

一旦显示,请参阅:

Hello, welcome to my calculator
Enter in some stuff you want to me to calculate
If you need help please type "help"
If at anytime you want to leave, type "quit"
If you want to continue, hit enter.

您的代码进入while循环,它不支持字符串输入。 您可以做的是添加一个打印声明,询问用户输入(&#34;帮助&#34;,&#34;退出&#34;或输入&#34;) 然后继续进行。

答案 2 :(得分:0)

实际上你的布尔退出在这种情况下是多余的。

您需要做的就是将循环更改为while (true){}并在用户输入“quit”时退出。看看这段代码。

不要调用nextDouble();将第一个输入作为字符串并检查是否退出。如果不是,则可以使用Double.parseInt()将其转换为双精度,否则将抛出数字格式异常如果用户输入为“退出”,您可以拨打system.exit(0);,也可以拨打return;

package calculator;

 import java.util.Scanner;

    public class Calculator {

    public static void main(String[] args) {

        System.out.println("Hello, welcome to my calculator");
        System.out.println("Enter in some stuff you want to me to calculate");

        Scanner scan = new Scanner(System.in);
        System.out.println("If you need help please type \"help\"");
        System.out.println("If at anytime you want to leave, type \"quit\"");
        System.out.println("If you want to continue, hit enter.");

        String s1 = scan.nextLine();

        if (s1.equals("help")){
            System.out.println(" ");
            System.out.println("Double operand commands:");
            System.out.println("Addition: '+' (Ex: 'a + b' )");
            System.out.println("Subtraction: '-' (Ex: 'a - b' )");
            System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
            System.out.println("Division: '/' (Ex: 'a / b' )");
            System.out.println("Exponents: '^' (Ex: 'a ^ b' )");
            System.out.println(" ");
        }

        Scanner input = new Scanner(System.in);

        Maths maths = new Maths();

        double answer = 0;
        double numA, numB;
        char operator;
        boolean quit = false;


         while (true) {
            System.out.print("Please enter your equation: ");
            String s=input.next();
            if(s.equals("quit")){
                System.out.println("Thanks for using my programe");
                System.exit(0);
            }
            numA = Double.parseDouble(s);
            operator = input.next().charAt(0);
            numB = input.nextDouble();        

            if (operator == '+') {
                answer = maths.add(numA, numB);
            }

            if (operator == '-') {
                answer = maths.subtract(numA, numB);
            }

            if (operator == '*') {
                answer = maths.multiply(numA, numB);
            }

            if (operator == '/') {
                answer = maths.divide(numA, numB);
            }

            if (operator == '^') {
                answer = maths.power(numA, numB);
            }

            System.out.println(answer);        



         }

        }

    }

    class Maths {

        double add(double a, double b) {
            double answer = a+b;
            return answer;          
        }

        double subtract(double a, double b) {
            double answer = a-b;
            return answer;          
        }

        double multiply(double a, double b) {
            double answer = a*b;
            return answer;          
        }

        double divide(double a, double b) {
            double answer = a/b;
            return answer;          
        }

        double power(double a, double b){
            double answer =a;

            for (int x=2; x<=b; x++){
                answer *= a;
            }

            return answer;
        }
}

示例输出&gt;&gt;

Hello, welcome to my calculator
Enter in some stuff you want to me to calculate
If you need help please type "help"
If at anytime you want to leave, type "quit"
If you want to continue, hit enter.

Please enter your equation: 2
+
3
5.0
Please enter your equation: quit
Thanks for using my programe

答案 3 :(得分:-1)

您的问题是Scanner无法使用.equals方法。您需要先识别一个字符串变量,将其设置为Scanner.ReadLine(),然后将其与&#34; quit&#34;进行比较。串。像这样:

Scanner scan = new Scanner(System.in);

String input = scan.ReadLine();

if(input.equals("quit"))
{
   //close the program
}

如果这有助于您,请告诉我。

答案 4 :(得分:-1)

也许是因为缓存。您可以在系统退出之前执行刷新。