Answer to Question #313837 in Java | JSP | JSF for emem

Question #313837

Problem: Make a program that will accept 2 numbers and a choice of operation (1 = Display Sum, 2 = Display Difference, 3 = Display Product, 4 = Display Quotient, and 5 = Display All). if the choice input is not from 1 to 5, reaccept an input again. Display the selected option by the user.

Example input and output:

Enter number: 5

Enter number: 2

[1] Display Sum

[2] Display Difference

[3] Display Product

[4] Display Quotient

[5] Display All

Input choice: -1

[1] Display Sum

[2] Display Difference

[3] Display Product

[4] Display Quotient

[5] Display All

Input choice: 10

[1] Display Sum

[2] Display Difference

[3] Display Product

[4] Display Quotient

[5] Display All

Input choice: 5

Sum : 7

Difference: 3

Product: 10

Quotient: 2

 

 



1
Expert's answer
2022-03-18T07:50:16-0400


import java.util.Scanner;


public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		System.out.print("Enter number: ");
		double number1 = keyBoard.nextDouble();
		System.out.print("Enter number: ");
		double number2 = keyBoard.nextDouble();
		System.out.println("[1] Display Sum");
		System.out.println("[2] Display Difference");
		System.out.println("[3] Display Product");
		System.out.println("[4] Display Quotient");
		System.out.println("[5] Display All");
		System.out.print("Input choice: ");
		int ch = keyBoard.nextInt();
		double sum = number1 + number2;
		double difference = number1 - number2;
		double product = number1 * number2;
		double quotient = number1 / number2;


		switch (ch) {
		case 1:
			System.out.println("Sum: " + sum);
			break;
		case 2:
			System.out.println("Difference: " + difference);
			break;
		case 3:
			System.out.println("Product: " + product);
			break;
		case 4:
			System.out.println("Quotient: " + quotient);
			break;
		case 5:
			System.out.println("Sum: " + sum);
			System.out.println("Difference: " + difference);
			System.out.println("Product: " + product);
			System.out.println("Quotient: " + quotient);
			break;
		default:
			System.out.printf("Wrong shoice\n\n");
			break;
		}
		keyBoard.close();
	}
}

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