如何让我的代码在结束后重新运行?

问题描述 投票:0回答:1

        switch(choice) {
            case 1: 
                double sum = 0;
                for (int i = 1; i <= 5; i++) {
                    System.out.println("Enter " + i + "th number: ");
                    double number = input.nextDouble();
                    sum = sum + number;
                }
                System.out.println("The sum of 5 numbers is " + sum);
            break;

            case 2:
                double subtract = 0;
                for (int i = 1; i <= 5; i++) {
                    System.out.println("Enter " + i + "th number: ");
                    double number = input.nextDouble();
                    subtract = subtract - number;
                }
                System.out.println("The subtract of 5 numbers is " + subtract);
            break;

            case 3:
                double multiply = 1;
                for (int i = 1; i <= 5; i++) {
                    System.out.println("Enter " + i + "th number: ");
                    double number = input.nextDouble();
                    multiply = multiply * number;
                }
                System.out.println("The multiply of 5 numbers is " + multiply);
            break;

            case 4:
                double divide = 1;
                for (int i = 1; i <= 5; i++) {
                    System.out.println("Enter " + i + "th number: ");
                    double number = input.nextDouble();
                    divide = divide / number;
                }
                System.out.println("The divide of 5 numbers is " + divide);
            break;

            case 5:
               System.exit(0);

            default:
                System.out.println("Invalid choice");
                System.out.println("Please enter a valid choice");
        }
    }
}

这是我的代码(一个简单的计算器)现在我希望它在进入默认 amd case 5 后重新运行,请告诉我我是否可以做任何事情我怎样才能使这段代码更短

我尝试使用该系统。东西,但我知道没有命令可以做我想做的事

所以,如果你能帮助我,请帮助我!这对我来说意义重大 :D

我尝试使用chatgpt但仍然没有答案

java intellij-idea switch-statement calculator
1个回答
0
投票

在主循环之前添加一个布尔值,例如:

boolean running = true;

尝试将代码放入“while”中:

while (running) {

然后添加到您的交换机:

case 6:
   running = false;
© www.soinside.com 2019 - 2024. All rights reserved.