尝试理解 Exit、Loop、Method 以及 If 和 Else

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

我试图了解如何使用方法、循环、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();
        }
    }
java loops if-statement methods exit
1个回答
2
投票

我已经更新了您的代码:

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
© www.soinside.com 2019 - 2024. All rights reserved.