starting out in a java course 2 wks done can anyone help with below

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hinesd
    New Member
    • Oct 2009
    • 2

    starting out in a java course 2 wks done can anyone help with below

    Consider a vending machine that offers the following options:
    [1] Get gum
    [2] Get chocolate
    [3] Get popcorn
    [4] Get juice
    [5] Display total sold
    Design a program that continually allows users to select from these options. When options 1-4 are selected, an appropriate message is to be displayed acknowledging their choice. For example, when option 3 is selected, the following message could be displayed:
    Here is your popcorn
    The program terminates when option 5 is selected, at which point the total number of each type of item sold is displayed. For example:
    3 items of gum were sold
    2 items of chocolate were sold
    6 items of popcorn were sold
    9 items of juice were sold
    If an option other than 1-5 is entered, an appropriate error message should be displayed, such as:
    Error, options 1-5 only!
  • Thewarchief
    New Member
    • Oct 2009
    • 1

    #2
    You would need to create a 'while' loop with several 'if' statements. For instance, you could have something like:

    Code:
    if(selected == 1)
       {
       System.out.println("Here is your gum");
       gum++;
       }
    else if(selected == 2)
       {
       //etc
       }
    and your final 'else' would catch any invalid integer. Your while loop would break once the integer '5' was entered.

    Comment

    • hinesd
      New Member
      • Oct 2009
      • 2

      #3
      Thanks for that much clearer now will have a go later to complete same.
      D.

      Comment

      Working...