Answer to Question #238852 in Java | JSP | JSF for Precioud

Question #238852

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


1
Expert's answer
2021-09-18T14:07:28-0400
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");
	        }
		}
		
	    
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS