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
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();
}
}
Comments
Leave a comment