You are on page 1of 2

import java.util.

Scanner;

public class Calculator {

private static Scanner scanner;

public static void main(String[] args) {


while (true)
{
scanner = new Scanner(System.in); //for the input from user
System.out.println("Select from the following choices.\n");
System.out.println("[0] Exit");
System.out.println("[1] Addition");
System.out.println("[2] Subtraction");
System.out.println("[3] Multiplication");
System.out.println("[4] Division");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

if (choice >= 1 && choice <= 4) {


System.out.print("Enter the first number: ");
double num1 = scanner.nextInt();

System.out.print("Enter the second number: ");


double num2 = scanner.nextInt();

double output = 0;

switch (choice) {

case 1:
output = num1 + num2;
break;

case 2:
output = num1 - num2;
break;

case 3:
output = num1 * num2;
break;

case 4:
output = num1 / num2;
break;
}
System.out.println("Answer is = " + output);
}
else {
if (choice == 0) {
System.out.println("EXIT");
System.exit(0);
} else {
System.out.println("You have entered wrong choice\n");
}
}
}
}
}

You might also like