如何让程序第二次运行?

时间:2014-08-17 00:34:14

标签: java

import java.util.Scanner;

public class Dice {

    public static void main(String[] args) {
        //I used 'print' instead of 'println' just to make it look a little cleaner in the console.
        System.out.print("Input your first number: ");
        Scanner sc1 = new Scanner(System.in);
        double num1 = sc1.nextInt();

        //I use doubles for my variables just in case the user wants to divide.

        System.out.print("Input your second number: ");
        Scanner sc2 = new Scanner(System.in);
        double num2 = sc2.nextInt();

        /* I used words rather than the actual symbols for my operators just to get practice using scanners for strings.
         * Until now I'd solely been using them for int variables. And also due to the small detail that before programming,
         * I had no idea what a modulo was and I felt that would be confusing to a random person.
         */

        System.out.println("What would you like to do with these numbers?(Add, Subtract, Multiply, Divide, or Check Divisibility): ");
        System.out.println("Simply type 'check' to check the divisibility of your two numbers.");
        Scanner sc3 = new Scanner(System.in);
        String str1 = sc3.nextLine().toUpperCase();

        /* toUpperCase to prevent the user from creating an error by typing their in put in a 'unique' way.
         *It took me several failures to finally look up toUpperCase. 
          */

        double num3;

        switch(str1) {
        case "ADD":
            num3 = num1 + num2;
            System.out.println("The sum is: " + num3);
        break;
        case "SUBTRACT":
            num3 = num1 + num2;
            System.out.println("The difference is: " + num3);
        break;
        case "MULTIPLY":
            num3 = num1 * num2;
            System.out.println("The product is: " + num3);
        break;
        case "DIVIDE": 
            num3 = num1 / num2;
            System.out.println("The quotient is: " + num3);
        break;
        case "CHECK":
            num3 = num1 % num2;
            System.out.println("The remainder is: " + num3);
        break;
        default:
            System.out.println("Invalid input. Please ensure that two numbers were entered and that you entered a valid math operation.");
        break;
        }//switch statement

    }//main method

}//class

如果我想在我的答案中添加另一个数字,我如何让我的代码再次运行?我只想尝试使用我的Java(我非常环保),如果我的问题太宽泛,我会提前道歉。

4 个答案:

答案 0 :(得分:4)

考虑以下小程序

boolean quit = false;
while(!quit) {
    System.out.print("Enter Something:");
    Scanner sc1 = new Scanner(System.in);
    String input = sc1.nextLine();
    if(input.compareToIgnoreCase("quit") == 0) {
        quit = true;
        continue;
    }
    System.out.println("You entered " + input);
}

在此示例中,我们一直要求他们输入内容并将其打印出来,除非输入是“退出”,在这种情况下我们使用continue语句跳过循环的其余部分并返回到顶部while循环并重新评估另一次迭代的条件。如果您输入'quit',这将评估为false并停止循环并退出程序。

来自程序的示例输入/输出。请注意,没有“您输入退出”,这是因为continue语句将我们带回了while循环的顶部。

Enter Something:hello
You entered hello
Enter Something:quit

现在你怎么能适应你的程序呢?下面是一个关于如何做一个输入的小样本

double num1 = 0;
String input1 = sc1.nextLine();
if(input1.compareToIgnoreCase("quit") == 0) {
    // quit was entered, leave the loop
    quit = true;
    continue;
}
try {
    num1 = Double.parseDouble(input1);
} catch(NumberFormatException e) {
    // user entered something that isnt a number, quit the program for now
    // you can change this to whatever behavior you like in the future
    quit = true;
    continue;
}

这可能会给您留下一些验证问题,例如“我想让我的用户在输入无效数字时再试一次”这些都可以使用这种方法,它可以引导您朝着正确的方向前进。

答案 1 :(得分:2)

请记住,main()是一种可调用的方法。您可以在main方法方法的末尾再次调用它,而不是使用whilefor循环。

// Put this at the end of your main method
System.out.print("Do you want to execute again? (yes/no)");
boolean repeat = sc1.nextLine().toUpperCase().equals("YES");
if (repeat) {
    main(null); // You're not using any arguments in main()
}

另行说明,您不需要sc1sc2sc3的全部三个。它们基本相同。您可以在任何地方使用sc1并完全删除sc2sc3

答案 2 :(得分:1)

//像这样的东西,然后询问是否要进行另一次运行,如果没有设置标志错误

boolean flag = true;

while(flag)
{


System.out.print("Input your first number: ");
Scanner sc1 = new Scanner(System.in);
double num1 = sc1.nextInt();

答案 3 :(得分:1)

您应该将所有逻辑放在循环中,这将授予您重复任务,直到达到条件。 也许你可以要求用户在他想退出你的程序时插入字符串“EXIT”。

在你的情况下,我会做这样的事情:

boolean exitFlag = false; 

do {
    // <put your logic here> 

    String answer = sc3.nextLine().toUpperCase();
    if (answer.equals("EXIT")) {
        exitFlag = true;
    }


} while(!exitFlag);