I need to create a program in Java that does the following: 1.Prompt the user fo
ID: 3632217 • Letter: I
Question
I need to create a program in Java that does the following:1.Prompt the user for two integers.
2.Display a menu with the following options:
1. Add numbers
2. Subtract numbers
3. Quit
3.Prompt the user for a single integer that is either 1,2, or 3.
4.If the user enters an invalid selection, redisplay the menu and prompt them again. Continue this until you get a valid response (1,2,or 3). You must use a loop to do this.
5.If the user selects ‘3’ then exit the program.
6.Perform the associated operation (add or subtract) on the previously entered two integers and display the result. Return to step ‘b’ and continue in this manner until the user selects ‘3’ to exit the program
Any assitance would be greatly appreciated....
Explanation / Answer
import java.util.*;
public class addsub
{
public static void main(String[] args)
{
int x = 0;
int y = 0;
int input = 0;
Scanner sc = new Scanner(System.in);
do{
System.out.println("1. Add numbers");
System.out.println("2. Subtract numbers");
System.out.println("3. Quit");
input = sc.nextInt();
if (input == 1 || input ==2){
System.out.print("Enter the first number ");
x = sc.nextInt();
System.out.print("Enter the second number ");
y = sc.nextInt();
}
if( input == 1){
System.out.printf("The result is %d ",x + y);
} else if(input == 2){
System.out.printf("The result is %d ",x - y);
} else if(input == 3){
System.exit(0);
}
}
while(input !=3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.