help counting choices

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 2
Reputation: nitric0 is an unknown quantity at this point 
Solved Threads: 0
nitric0 nitric0 is offline Offline
Newbie Poster

help counting choices

 
0
  #1
Oct 19th, 2008
hey guys, i'm really stuck on this program. It's basically a survey and I have to ask people what drinks they like. 1-4, coffee tea oj and lemonade.

i'm having trouble counting the TOTAL NUMBER OF PEOPLE and counting how many people pick coffee tea oj or lemonade.

i have the 5th option set as a sentinel, where the program ends.

i need help like i said counting the total number of people who participated(person who quits doesn't count) and counting each drink

help?

import java.util.Scanner;
public class Assignment3
{
	public static void main(String[]args)
	{
		int choice = 1;
		int person = 1;
		int person1 = 0;
		int choice1 = 1, choice2 = 1, choice3 = 1, choice4 = 1;
		int total = 0, votes;
		double percent, perc1, perc2, perc3, perc4;
		
		Scanner keyboard = new Scanner(System.in);

		
		while (choice != 5)
		{
			System.out.println("Enter choice for person# " + person);
			System.out.println("1. Coffee		2. Tea		3. Orange Juice		4. Lemonade		5. Quit");
			choice = keyboard.nextInt();
			if(choice < 1 || choice  > 5)
				{
					System.out.print("That was an invalid choice -- enter 1 through 5 only");
					choice = keyboard.nextInt();
				}
				if(choice == choice1)
					person++;
					else 
						if(choice == choice2)
							person++;
								else
									if(choice == choice3)
										person++;
											else
												if(choice == choice4)
													person++;
					
		}
		person=choice1+choice2+choice3+choice4;
		perc1 = choice1/person;
		perc2 = choice2/person;
		perc3 = choice3/person;
		perc4 = choice4/person; 
			
			
		System.out.println("The total number of people surveyed: " + person);
		System.out.println("THe results are as follows");
		System.out.println();
		
		System.out.println(" Beverage				Number of Votes			Percentage");
		System.out.println("-------------------------------------------------------------------------------------");
		System.out.println();
		System.out.println("Coffee                	"+choice1+ "			    "+perc1);
		System.out.println("Tea								"+choice2+"					 "+perc2);
		System.out.println("Orange Juice 				"+choice3+"					 "+perc3);
		System.out.println("Lemonade						"+choice4+"					 "+perc4);
		
	}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 59
Reputation: Chaster is an unknown quantity at this point 
Solved Threads: 3
Chaster Chaster is offline Offline
Junior Poster in Training

Re: help counting choices

 
0
  #2
Oct 19th, 2008
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.
Last edited by Chaster; Oct 19th, 2008 at 8:43 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: help counting choices

 
0
  #3
Oct 20th, 2008
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
  1. person = choice1+ .... etc

here's the gist of what you need
  1. int choice1 = 0, choice2 = 0, choice3 = 0, choice4 = 0, person = 0;
  2.  
  3. //Leave original code here
  4.  
  5. if(choice < 1 || choice > 5) {
  6. System.out.print("That was an invalid choice -- enter 1 through 5 only");
  7. choice = keyboard.nextInt();
  8. }
  9. //You can add this and make it easier
  10. else {
  11. //We added a new preson
  12. person++;
  13. //Now we'll add their choice
  14. switch(choice){
  15. case 1:
  16. choice1++;
  17. break;
  18. case 2:
  19. choice2++;
  20. break;
  21. //continue on with others
  22. }
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: help counting choices

 
0
  #4
Oct 20th, 2008
  1.  
  2. // ...
  3.  
  4. System.out.println(" Beverage Number of Votes Percentage");
  5. System.out.println("-------------------------------------------------------------------------------------");
  6. System.out.println();
  7. System.out.println("Coffee "+choice1+ " "+perc1);
  8. System.out.println("Tea "+choice2+" "+perc2);
  9. System.out.println("Orange Juice "+choice3+" "+perc3);
  10. System.out.println("Lemonade "+choice4+" "+perc4);

Use tabs instead of trying to space things out and match them up manually =/
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 411 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC