如何重启java程序

时间:2017-06-18 12:35:00

标签: java switch-statement application-restart

我对java很新,只是玩弄基础知识。我做了一些计算器。 (可能更容易计算器,只是想到了我的想法)

这是我的代码:

import java.util.Scanner;

public class App {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        double second;

        System.out.println("Please tell me a number");

        double first = input.nextDouble();
        input.nextLine(); // Consume newline left-over

        System.out.println("You chose the number " + first);

        System.out
                .println("Now please choose what you want to do with the number. ( divide, add, multiply or subtract )");
        String text = input.nextLine();

        switch (text) {
        case "divide":
            System.out.println("What do you want to divide your number " + first + " with?");
            second = input.nextDouble();
            double resultDivide = first / second;
            System.out.println(first + " / " + second + " = " + resultDivide);
            break;
        case "add":
            System.out.println("What do you want to add to your number " + first + "?");
            second = input.nextDouble();
            double resultAdd = first + second;
            System.out.println(first + " + " + second + " = " + resultAdd);
            break;
        case "multiply":
            System.out.println("What do you want to multiply your number " + first + " with?");
            second = input.nextDouble();
            double resultMultiply = first * second;
            System.out.println(first + " * " + second + " = " + resultMultiply);
            break;
        case "subtract":
            System.out.println("What do you want to subtract your number " + first + " with?");
            second = input.nextDouble();
            double resultSubtract = first - second;
            System.out.println(first + " - " + second + " = " + resultSubtract);
            break;
        default:
            System.out.println("Sorry, I did not quite understand that. Please try again.");

        }

    }

所以我的问题是:当它在switch case语句中达到默认值时,我可以从begginning重启程序吗?

提前致谢。

3 个答案:

答案 0 :(得分:1)

似乎所有你想要做的就是循环,直到用户指定了有效的操作。在这种情况下,您可以使用while循环,观察变量:

import java.util.Scanner;

public class App {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        double second;
        System.out.println("Please tell me a number");
        double first = input.nextDouble();
        input.nextLine(); // Consume newline left-over

        System.out.println("You chose the number " + first);

        System.out
                .println("Now please choose what you want to do with the number. ( divide, add, multiply or subtract )");
        String text = input.nextLine();

        boolean correctOperation = true;

        do {
            correctOperation = true;

            switch (text) {
                case "divide":
                    System.out.println("What do you want to divide your number " + first + " with?");
                    second = input.nextDouble();
                    double resultDivide = first / second;
                    System.out.println(first + " / " + second + " = " + resultDivide);
                    break;
                case "add":
                    System.out.println("What do you want to add to your number " + first + "?");
                    second = input.nextDouble();
                    double resultAdd = first + second;
                    System.out.println(first + " + " + second + " = " + resultAdd);
                    break;
                case "multiply":
                    System.out.println("What do you want to multiply your number " + first + " with?");
                    second = input.nextDouble();
                    double resultMultiply = first * second;
                    System.out.println(first + " * " + second + " = " + resultMultiply);
                    break;
                case "subtract":
                    System.out.println("What do you want to subtract your number " + first + " with?");
                    second = input.nextDouble();
                    double resultSubtract = first - second;
                    System.out.println(first + " - " + second + " = " + resultSubtract);
                    break;
                default:
                    System.out.println("Sorry, I did not quite understand that. Please try again.");
                    correctOperation = false;
                    text = input.nextLine();
            }
        } while(!correctOperation)
    }
}

答案 1 :(得分:0)

您需要while循环。循环的输入条件是default情况。在循环内部,您必须放置以前使用的这段代码:

System.out.println("Now please choose what you want to do with the number. ( divide, add, multiply or subtract )");
        String text = input.nextLine();

因此,在您没有得到default案例之前,它会继续询问输入。

答案 2 :(得分:0)

我认为你需要一个while循环,例如:

String text = input.readLine();
while(text != null) {
   switch(...)
} 

而且,我想给你一些建议:

  • 如果为null,则应检查输入的“text”字符串,如果“text”字符串为null,则会遇到NullPointerException。
  • 如果你要使用这么多的case语句,你必须考虑设计模式,链接是Replacing if else statement with pattern