Try this:
have a variable for each drink (you can have an array as well, if you want) and proceed as follows:
- when a new person comes, increment the person variable
- read their choice
- if choice = 1, increment the variable for tea, if choice = 2, inc. variable for coffee etc
- at the end, you will know that:
- you have asked 'person' persons
- teaVariable of them prefer tea
- coffeeVariable of them prefer coffee
- etc.
hope this helps.
Chaster
Junior Poster in Training
68 posts since Jun 2007
Reputation Points: 12
Solved Threads: 3
You seem to be making things a little hard. Zero things out to start with, then if their choice is valid increment person, then record their choice, then do your calculations. Also remove your line of
person = choice1+ .... etc
here's the gist of what you need
int choice1 = 0, choice2 = 0, choice3 = 0, choice4 = 0, person = 0;
//Leave original code here
if(choice < 1 || choice > 5) {
System.out.print("That was an invalid choice -- enter 1 through 5 only");
choice = keyboard.nextInt();
}
//You can add this and make it easier
else {
//We added a new preson
person++;
//Now we'll add their choice
switch(choice){
case 1:
choice1++;
break;
case 2:
choice2++;
break;
//continue on with others
}
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143