Return 到 Java 代码的开始(计算器的主菜单)

Return to start of the Java code(main menu of calculator)

我是新手,对 Java 有点菜鸟,我正在尝试制作一个计算器,没有测试所有内容以查看它是否有效,但我遇到了问题。我似乎无法弄清楚如何在执行一次计算后返回主菜单。我在最后添加了该问题,以提示用户退出或继续返回主菜单。我只是不知道在 if(whatnow == Y){ 中放什么我应该怎么做才能回到主菜单? }.对不起,如果它有点长或什么的,但它们真的都是一样的,所以跳过计算吧。任何帮助表示赞赏。我是 java 的新手,我可能不得不重新编写这段代码。

package practice;

import java.util.Scanner;

public static void main(String[] args){

    Scanner scan = new Scanner(System.in);

    int choice;
    int firstnumber;
    int secondnumber;
    int result;
    char whatnow;

    System.out.println("Welcome to StemCalc Z Edition(Integers only)!");
    System.out.println("Made with love and basic Java");
    System.out.println("Which math operation would you like to perform?");
    System.out.println("");
    System.out.println("WARNING: Enter the integer x, press ENTER, then enter y");
    System.out.println("");
    System.out.println("[1]-Addition (+)");
    System.out.println("[2]-Subtraction (-)");
    System.out.println("[3]-Multiplication (x)");
    System.out.println("[4]-Division (/)");
    System.out.println("");
    System.out.println("Enter your choice[1-4 or 99]:"); choice = scan.nextInt();

    while ((choice < 1 || choice > 4) && choice != 99) {
        System.out.println("Please enter 1, 2, 3, 4, or 99: ");
        choice = scan.nextInt();
    }

    if (choice == 1){
        System.out.println("Enter two integer to add(x + y)");

        firstnumber = scan.nextInt();
        secondnumber = scan.nextInt();
        result = firstnumber + secondnumber;

        System.out.println(firstnumber + " + " + secondnumber + " = " + result);
    }

    else if (choice == 2) {
        System.out.println("Enter two integers to subtract(x - y)");

        firstnumber = scan.nextInt();
        secondnumber = scan.nextInt();
        result = firstnumber - secondnumber;

        System.out.println(firstnumber + " - " + secondnumber + " = " + result);
    }

    else if (choice == 3) {
        System.out.println("Enter two integers to multiply(x * y)");

        firstnumber = scan.nextInt();
        secondnumber = scan.nextInt();
        result = firstnumber * secondnumber;

        System.out.println(firstnumber + " * " + secondnumber + " = " + result);
    }

    else if (choice == 4) {
        System.out.println("Enter to integers to divide(x / y)");

        firstnumber = scan.nextInt();
        secondnumber = scan.nextInt();

        while (secondnumber == 0) {
            System.out.println("ERROR-CANNOT DIVIDE TO ZERO! Type another integer:");
            secondnumber = scan.nextInt();
        }

        result = firstnumber / secondnumber;

        System.out.println(firstnumber + " / " + secondnumber + " = " + result);
        }

    else if (choice == 99) {
        System.exit(0);

    }

    while (choice !=99) {
        System.out.println("Do you want to continue calculating? [Y/N]:"); whatnow = scan.next().charAt(0);

        if (whatnow == 'Y' || whatnow == 'y') {

        }

        if (whatnow == 'N' || whatnow == 'n') {
            System.exit(0);
        }
    }
}

}

P.S:我将结尾编辑成这样,开头有一段时间(真):

`while (choice !=99) {
            System.out.println("Do you want to continue calculating? [Y/N]:"); whatnow = scan.next().charAt(0);

            while(whatnow != 'Y' || whatnow != 'y' || whatnow !='N' || whatnow !='n') {
     System.out.println("Enter [Y/N] only:"); whatnow = scan.next().charAt(0);
            }

            if (whatnow == 'N' || whatnow == 'n') {
                System.exit(0);`

与其尝试在末尾放置一个 while 循环,不如尝试将整个代码放在一个 while(true) 块中。在末尾使用 if 语句,您可以询问用户是否要继续我们的 not。使用 break 关键字退出循环。 尝试这样退出:

if(input == 'n'){break;}

这样做是为了退出 while 循环。您可以在 while 块之外执行更多指令。

您只需要重复您所写的所有内容,直到用户插入 N。所以你要做的就是把所有东西都放在一个 while(true) 循环中,它的最后一条指令是:

if (whatnow == 'N' || whatnow = 'n') {
    System.exit(0);
}

这样,如果用户插入 Nn 以外的任何内容,循环会将他带回主菜单打印部分,因此您可能需要对值添加测试whatnow 的方法与 choice.

的方法相同

结果会是这样的:

public static void main(String[] args){

    ...

    while(true){
        System.out.println("Welcome to StemCalc Z Edition(Integers only)!");

        ...

        while (choice !=99) {
            System.out.println("Do you want to continue calculating? [Y/N]:"); whatnow = scan.next().charAt(0);

            //insert some loop to ensure that the value of whatnow will be either Y or N

            if (whatnow == 'N' || whatnow == 'n') {
                System.exit(0);
            }
        }
    }
}

编辑

这是我在上一条评论中预期的示例代码:

public static void main(String[] args){
    char whatnow = 'Y';
    Scanner scan = new Scanner(System.in);

    while (whatnow != 'N' && whatnow != 'n') {
        int choice = printMenuAndAsk(scan);

        if (choice == 99)
            break;
        else performOperation(choice, scan);

        System.out.println("Do you want to continue calculating? [Y/N]:"); 
        whatnow = scan.next().charAt(0);

        while(whatnow != 'N' && whatnow != 'Y' && whatnow != 'n' && whatnow != 'y') { 
            System.out.println("Incorrect answer");
            whatnow = scan.next().charAt(0);
        }
    }   
    scan.close();   
}

public static int printMenuAndAsk(Scanner scan) {
    int choice;

    System.out.println("Welcome to StemCalc Z Edition(Integers only)!");
    ...
    System.out.println("Enter your choice[1-4 or 99]:"); 
    choice = scan.nextInt();

    while ((choice < 1 || choice > 4) && choice != 99) {
        System.out.println("Please enter 1, 2, 3, 4, or 99: ");
        choice = scan.nextInt();
    }

    return choice;
}

public static void performOperation(int operation, Scanner scan) {
    System.out.println("Enter first:");
    int firstnumber = scan.nextInt();
    System.out.println("Enter second:");
    int secondnumber = scan.nextInt();

    if (choice == 1)
        System.out.println(firstnumber + " + " + secondnumber + " = " + (firstnumber+secondnumber));
    else if (choice == 2) 
        System.out.println(firstnumber + " - " + secondnumber + " = " + (firstnumber-secondnumber));
    else if (choice == 3) 
        System.out.println(firstnumber + " * " + secondnumber + " = " + (firstnumber*secondnumber));
    else if (choice == 4) {
        while (secondnumber == 0) {
            System.out.println("ERROR-CANNOT DIVIDE TO ZERO! Type another integer:");
            secondnumber = scan.nextInt();
        }
        System.out.println(firstnumber + " / " + secondnumber + " = " + (firstnumber/secondnumber));
    }
}

您可以按如下方式更改:

你的主要方法是这样的:

public static void main(String[] args) {
        calc();
    }

然后创建一个名为 calc():

的方法
public static void calc() {

        Scanner scan = new Scanner(System.in);

        //the rest of your code

 } else if (choice == 99) {
            System.exit(0);

        }

        calc(); //call the method again at the end of your code
                //remove your while loop

    }