I am trying to understand how exactly you are meant to prompt a user for two numbers and then list a menu for them to choose the course of action they want to do with those numbers however I am quite new to java and do not understand the concept very well. If someone could help me the specifics are below as well as the code that I have so far.

  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.
  7. Name the class CalcJava and save program as CalcJava.java
  8. Submit the program using the WA3 link above.

    import java.util.Scanner;
    public class CalcJava
    {
    public static void main(String[] args)
    {
    int num1;
    int num2;
    Scanner inputDevice = new Scanner(System.in);
    System.out.print("Please enter a number:");
    num1 = inputDevice.nextInt();
    System.out.print("Please enter a second number: ");
    num2 = inputDevice.nextInt();
    System.out.println("Your numbers are: " + num1 + " and "+ num2);

    }
    

    }

I know that it is not much but this is the point that I have gotten to so far. If someone could point me in the right direction then it would be a big help.

Thank you in advance.

OK, you are done with #1.

For number 2, you simply create a public static method inside your Calc class but not inside the main() method. You could call it displayMenu() or alike.

For number 3, you could also create another public static method to accept user's input. This time, however, you should return a valid value back to the caller. If the input is not valid, ask the user again. This method should be called inside the main(). Also, you must create a new variable in the main() to take the user's choice.

For number 4, you need a type of while loop. This process must be inside #3. I would suggest you to use a do-while loop because a user must enter a value at least once. The condition is that if the choice entered is not between 1 and 3, or it is not a number, keep the program inside the loop.

Once you get #4 right, return the value at the end of #3.

For number 5, you need another do-while loop around your current implementation. Check for the condition. If the choice is not equal to 3, keep looping.

For number 6, you need an if/else if statement to perform whatever the program is supposed to do. If the choice is equal to 1, do the add. Else if the choice is equal to 2, do the subtract. There should not be else statement here.

A simple skeleton of your program is below. It still needs a lot of work and that's your job to complete it. Hope this would at least give you more clarification.

class Calc {
  ...

  public static void displayMenu() { // no value is return(void)
    ...  // display the menu using System.out.println()
  }  // displayMenu()

  public static int getChoice() {  // look at return type, it is int
    int choice;  // declare variable
    do {
      displayMenu();  // first display menu
      ...             // then accept user input, handle invalid input, etc.
    } while(THE_VALUE_IS_BETWEEN_1_AND_3);

    return choice;    // must return the same type
  }  // getChoice()

  ...

  public static void main(String[] args) {
    ...  // first 3 lines of your current implementation goes here
    int choice;  // a place holder to take user's choice
    do {
      //====== your current implementation
      System.out.print("Please enter a number:");
      num1 = inputDevice.nextInt();
      System.out.print("Please enter a second number: ");
      num2 = inputDevice.nextInt();
      System.out.println("Your numbers are: " + num1 + " and "+ num2);
      //====== end the part of your current implementation
      choice = getChoice();  // call the method to display the menu
      ... // now use if/else if condition to deal with whatever the program
      ... // is supposed to do
    } while(THE_CONDITION_IS_IF_CHOICE_IS_NOT_EQUAL_TO_3);
  }  // main()
}

After you print out your values, just print out the menu, then get the user value from the input device. With the value taken, you can use a if statement or a switch statement to do subtraction or addition to the first two values or quit the program.

Ok I can get the user input for the two integers and I can get the menu to display but each time I choose my option it just loops to the menu again. How can I fix this?

import java.util.Scanner;
public class CalcJava
{

public static void displayMenu()
{
    System.out.println("What would you like to do with the numbers you just entered:");
    System.out.println("1. Add numbers");
    System.out.println("2. Subtract numbers");
    System.out.println("3. Quit");
    System.out.println("Please enter a choice of 1, 2, or 3.");
} 

public static int getChoice()
{
    int choice;

    do
    {
        Scanner inputDevice = new Scanner(System.in);

        displayMenu();
        System.out.println("\nWhat would you like to do: ");
        choice = inputDevice.nextInt();
        if(choice > 3|| choice < 1)
            System.out.println("This is an invalid choice");
    } while(choice <= 3);

    return choice;
}



public static void main(String[] args)
{
    int num1;
    int num2;
    int answer;
    Scanner inputDevice = new Scanner(System.in);
    int choice;
    do
    {
        System.out.print("Please enter a number:");
        num1 = inputDevice.nextInt();
        System.out.print("Please enter a second number: ");
        num2 = inputDevice.nextInt();
        System.out.println("Your numbers are: " + num1 + " and "+ num2 +"\n");
        choice = getChoice();
        if(choice == 1)
            answer = num1 + num2;
        else 
            answer = num1 -num2;
        if(choice == 3)
            System.out.print("Thank you for using my program Have a nice day!");
    } while (choice !=3);
}

}

your condition at line 27 is causing this, if your input is valid it will always pass the loop condition thus printing the menu again

So the the statement in line 27 needs to be between 1 and 3 or is it something different? What exactly is it supposed to say in order to call the choice options?

If the condition in line 27 is NOT between 1 and 3, you keep looping it. Currently, if the value is less than or equal to 3, it will be looping. Sorry for giving you a wrong condition.

Line 51, you must NOT use else but rather else if() instead. The reason is that if a user enter 3, it will perform that choice.

If the condition in line 27 is NOT between 1 and 3, you keep looping it. Currently, if the value is less than or equal to 3, it will be looping. Sorry for giving you a wrong condition.

There is no need to apologize you are spending your own personal time trying to help someone you don't even know. I am sorry to be such a pain but now when I run the program the only option that is working correctly is option #3. If I try to add or subtract the two numbers that I input all a get back is the prompt from the beginning of the program asking for my numbers again. How can I solve this problem?

import java.util.Scanner;
public class CalcJava
{

public static void displayMenu()
{
    System.out.println("What would you like to do with the numbers you just entered:");
    System.out.println("1. Add numbers");
    System.out.println("2. Subtract numbers");
    System.out.println("3. Quit");
    System.out.println("Please enter a choice of 1, 2, or 3.");
} 

public static int getChoice()
{
    int choice;

    do
    {
        Scanner inputDevice = new Scanner(System.in);

        displayMenu();
        System.out.println("\nWhat would you like to do: ");
        choice = inputDevice.nextInt();
        if(choice > 3|| choice < 1)
            System.out.println("This is an invalid choice!");
    } while(choice > 3 || choice < 1);

    return choice;
}



public static void main(String[] args)
{
    int num1;
    int num2;
    int answer;
    Scanner inputDevice = new Scanner(System.in);
    int choice;
    do
    {
        System.out.print("Please enter a number:");
        num1 = inputDevice.nextInt();
        System.out.print("Please enter a second number: ");
        num2 = inputDevice.nextInt();
        System.out.println("Your numbers are: " + num1 + " and "+ num2 +"\n");
        choice = getChoice();
        if(choice == 1)
            answer = num1 + num2;
        else if(choice == 2) 
            answer = num1 -num2;
        if(choice == 3)
            System.out.print("Thank you for using my program. Have a nice day!");
    } while (choice !=3);
}

}

Return to step ‘b’ and continue

Oh, I thought that step b is to ask for a new set of numbers. Does it mean to ask for a new choice?

Yes, the user is to input 2 numbers and then the menu displays. The user is then asked to make a choice between adding or subtracting the two integers entered or they can just quit when selecting choice 3. If they choose to add or subtract then once the answer is given the menu needs to display again and preform the desired operation until they choose to quit.

Then you simply move the code portion that accept 2 numbers out above the do-while loop (the portion from your original code).

Thank you for all of your help. I also needed to add in some print statements to actually have the result appear. I don't know how I missed that part:). The program is now working correctly and is finished.

Thanks so much for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.