Connecting Tech Pros Worldwide Help | Site Map

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

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: 4 Weeks Ago
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!
Newbie
 
Join Date: Oct 2009
Posts: 1
#2: 4 Weeks Ago

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


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

Expand|Select|Wrap|Line Numbers
  1. if(selected == 1)
  2.    {
  3.    System.out.println("Here is your gum");
  4.    gum++;
  5.    }
  6. else if(selected == 2)
  7.    {
  8.    //etc
  9.    }
and your final 'else' would catch any invalid integer. Your while loop would break once the integer '5' was entered.
Newbie
 
Join Date: Oct 2009
Posts: 2
#3: 4 Weeks Ago

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


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