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

Tags: , , , ,



Hello guys I need help with my code. I am trying to understand how to use the method, loop,if-else statement, and exit code. So I’m writing a simple calculation base on the user choice but right now I can’t figure out how to make the input read to loop back when user input else than the number (mean no alphabet are allowed) and it will continue back to the option till the user enter the right option that is either 1 or 2.

Do let me know if I make any mistake or is there a way to simplify this code more.

WANT OUTPUT TO BE LIKE THIS:-

[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

CODE:-

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();
        }
    }

Answer

I have updated your code :

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();
    }

}

Output :

[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


Source: stackoverflow