Create a console calculator application that make use of a menu it should gives a user an option to end a program when she entered a certain option
import java.util.Scanner;
public class Main
{
public static void add(){
Scanner in=new Scanner(System.in);
int x,y;
System.out.println("Enter first number: ");
x=in.nextInt();
System.out.println("Enter second number: ");
y=in.nextInt();
System.out.println(x+" + "+y+" = "+(x+y));
}
public static void sub(){
Scanner in=new Scanner(System.in);
int x,y;
System.out.println("Enter first number: ");
x=in.nextInt();
System.out.println("Enter second number: ");
y=in.nextInt();
System.out.println(x+" - "+y+" = "+(x-y));
}
public static void mul(){
Scanner in=new Scanner(System.in);
int x,y;
System.out.println("Enter first number: ");
x=in.nextInt();
System.out.println("Enter second number: ");
y=in.nextInt();
System.out.println(x+" * "+y+" = "+(x*y));
}
public static void div(){
Scanner in=new Scanner(System.in);
int x,y;
System.out.println("Enter first number: ");
x=in.nextInt();
System.out.println("Enter second number: ");
y=in.nextInt();
System.out.println(x+" / "+y+" = "+(x/y));
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
while(true){
int c;
System.out.println("Choose an operation:\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n0. Exit");
c=in.nextInt();
if (c==1){
add();
}
else if (c==2){
sub();
}
else if (c==3){
mul();
}
else if (c==4){
div();
}
else if (c==0){
break;
}
else{
System.out.println("Invalid option");
}
}
}
}
Comments
Leave a comment