试图理解退出、循环、方法以及 If 和 Else [JAVA]

Trying to understanding Exit, Loop, Method and If and Else [JAVA]

大家好,我的代码需要帮助。我试图了解如何使用方法、循环、if-else 语句和退出代码。所以我正在根据用户的选择编写一个简单的计算,但现在我无法弄清楚当用户输入数字 以外的内容时如何使输入读取循环返回(意味着不允许使用字母表) 它将继续回到选项,直到用户输入正确的选项,即 1 或 2。

如果我有任何错误或有什么方法可以进一步简化此代码,请告诉我。

想要这样的输出:-

[1] Calculation 
[2] Exit
Your choice: a

Please choose only 1 or 2

[1] Calculation 
[2] Exit
Your choice: 1

Enter 1st number: 1 
Enter 2nd number: 1 
Total: 2

代码:-

import java.util.Scanner;
    public class Testing {
    
        int ans;
        boolean Loop = true;
    
        public void SimpleCalculation() {
    
            Scanner input = new Scanner(System.in);
    
            while (Loop) {
                System.out.println("[1] Calculation ");
                System.out.println("[2] Exit");
                System.out.print("Your choice: ");
                ans = input.nextInt();
    
                if (ans == 1) {
                    System.out.print("Enter 1st number: ");
                    int number1 = input.nextInt();
    
                    System.out.print("Enter 2nd number: ");
                    int number2 = input.nextInt();
                    
                    int result = number1 + number2;
                    System.out.println("Total: " + result);
    
                } else if (ans == 2) {
                    System.out.println("Thank you");
                    input.close();
                    break;
                } else {
                    System.out.println("Please choose only 1 or 2");
                }
            }
             System.exit (0);
        }
        
        public static void main(String[] args) {
    
            Testing t = new Testing();
            t.SimpleCalculation();
        }
    }

我已经更新了你的代码:

public class Testing {

    public static void SimpleCalculation() {
        boolean Loop = true;
        
        Scanner input = new Scanner(System.in);

        while (Loop) {
            System.out.println("[1] Calculation ");
            System.out.println("[2] Exit");
            System.out.print("Your choice: ");
            while(!input.hasNextInt()) {
                System.out.println("Please choose only 1 or 2");
                input.nextLine();
                continue;
            }
            int ans = input.nextInt();

            if (ans == 1) {
                System.out.print("Enter 1st number: ");
                int number1 = input.nextInt();

                System.out.print("Enter 2nd number: ");
                int number2 = input.nextInt();
                
                int result = number1 + number2;
                System.out.println("Total: " + result);
            } else if (ans == 2) {
                System.out.println("Thank you");
                input.close();
                break;
            } else {
                System.out.println("Please choose only 1 or 2");
            }
        }
        
    }
    
    public static void main(String[] args) {
        SimpleCalculation();
    }

}

输出:

[1] Calculation 
[2] Exit
Your choice: a
Please choose only 1 or 2
[1] Calculation 
[2] Exit
Your choice: 1
Enter 1st number: 1
Enter 2nd number: 2
Total: 3