Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im writting a program that prompts the user for two integers. Then displays a me

ID: 3641496 • Letter: I

Question

Im writting a program that prompts the user for two integers. Then displays a menu asking them to pick: 1. Addition 2. Subtraction 3. Quit. If they enter a number not 1-3 it asks them to enter it again.

I got all of that. My problem after that it is supposed to return to the menu until they select 3 to quit. I cannot get it to work. Can anyone help me? Here is what I have:

import java.util.Scanner;

public class CalcJava {


public static void main(String[] args) {

int num1,num2;
int choice=0;
char responseChar;
String response;

Scanner input = new Scanner(System.in);
// Ask the user for two integers.
System.out.println("Enter first integer: ");
num1=input.nextInt();
System.out.println("Enter second integer: ");
num2=input.nextInt();

do
{
while(choice<1 || choice >3)
{

System.out.println("Please pick a number from the menu: ");
System.out.println("1. Add numbers 2. Subtract numbers 3. Quit");

System.out.println("Enter your choice(1-3): ");
choice = input.nextInt();

if (choice >3)
System.out.println("You entered an invalid selection");

if(choice==3)
System.exit(1);

if(choice==1)
System.out.println("Result: "+(num1+num2));
else if(choice==2)
System.out.println("Result: "+(num1-num2));
}

System.out.println("Do you want to go again?");
response=input.nextLine();
responseChar = response.charAt(0);

{
while(responseChar=='y');

}
}
}

Explanation / Answer

Your do-while statement is wrong. It is much simpler if you just use a while(true) statement. So get rid of do { while(choice3) { ... and just make it: while(true){ ...//your main code in here if(choice==3) break;//break will get you out of the while(true) loop ... }