Java 我希望计算器重新启动,直到按下';q';信

Java 我希望计算器重新启动,直到按下';q';信,java,Java,我写了一个简单的计算器,我希望计算器重新启动,直到我按下“q”按钮。问题是我不能让它那样做 我尝试了一些do/while循环,但总是出错 import java.util.Scanner; public class Assignment2_3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(true) { S

我写了一个简单的计算器,我希望计算器重新启动,直到我按下“q”按钮。问题是我不能让它那样做

我尝试了一些do/while循环,但总是出错

import java.util.Scanner;

public class Assignment2_3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(true) {
            System.out.println("Welcome to my mini calculator program!");
            System.out.println("Please enter the numbers along operation (press q to exit):");
            int number1 = in.nextInt();
            char typeoftheoperation = in.next().charAt(0);
            int number2 = in.nextInt();
            String terminate = "";
            double result;
            switch (typeoftheoperation) {
                case ('+'):
                    result = number1 + number2;
                    System.out.println("The result is :" + result);
                    break;

                case '-':
                    result = number1 - number2;
                    System.out.println("The result is :" + result);
                    break;

                case '*':
                    result = number1 * number2;
                    System.out.println("The result is :" + result);
                    break;

                case '/':
                    result = number1 / number2;
                    System.out.println("The result is :" + result);
                    break;
                terminate = in.next();
            if (terminate.equals("q"))
                in.close();
            }



        }
    }
}

我希望计算器一直运行,直到我决定输入“q”,然后它应该停止。

以下是您所问问题的临时解决方案。 您可能还想使用try…catch。。。解决“输入不匹配异常”。 但这将暂时奏效

    import java.util.Scanner;

    public class Assignment2_3 {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while(true) {
                System.out.println("Welcome to my mini calculator program!");
                System.out.println("Please enter the numbers along operation (press q to exit):");
                int number1 = in.nextInt();
                char typeoftheoperation = in.next().charAt(0);
                int number2 = in.nextInt();
                String terminate = "";
                double result;
                switch (typeoftheoperation) {
                    case ('+'):
                        result = number1 + number2;
                        System.out.println("The result is :" + result);
                        break;

                    case '-':
                        result = number1 - number2;
                        System.out.println("The result is :" + result);
                        break;

                    case '*':
                        result = number1 * number2;
                        System.out.println("The result is :" + result);
                        break;

                    case '/':
                        result = number1 / number2;
                        System.out.println("The result is :" + result);
                        break;
                }
                terminate = in.next();
                if (terminate.equals("q")) {
                    in.close();
                    System.out.println("You turned off the calculator! See you again");
                    break;
                }



            }
        }
    }

选项
从何而来?还有一个大问题:您正在使用
nextInt()
读取第一个数字-这永远不会接受字母
q
-最好只读取一行,然后检查它是否是
q
;如果不是,则解析它(
split()
Integer.parseInt()
)。或者,在计算后询问,如果用户想继续或不想感谢您的回答,我会做一点,然后重新发布新的code@Tom非常感谢,我会为此感到抱歉的!顺便说一句不要关闭你没有打开的东西。您正在关闭系统登录。如果在开始时按“q”,这也将不起作用。您必须执行此操作,然后只有您才能退出计算器。如果你想换一种方式做,那就需要做一些工作谢谢。你认为只使用数组和字符串更好吗?这仍然给了我一个错误。我认为我应该使用字符串和数组。是的,如果你使用字符串数组获取输入并检查所有输入并相应地采取行动,会更好。