473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help counting CHOICES

2
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?

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Assignment3
  3. {
  4.     public static void main(String[]args)
  5.     {
  6.         int choice = 1;
  7.         int person = 1;
  8.         int person1 = 0;
  9.         int choice1 = 1, choice2 = 1, choice3 = 1, choice4 = 1;
  10.         int total = 0, votes;
  11.         double percent, perc1, perc2, perc3, perc4;
  12.  
  13.         Scanner keyboard = new Scanner(System.in);
  14.  
  15.  
  16.         while (choice != 5)
  17.         {
  18.             System.out.println("Enter choice for person# " + person);
  19.             System.out.println("1. Coffee        2. Tea        3. Orange Juice        4. Lemonade        5. Quit");
  20.             choice = keyboard.nextInt();
  21.             if(choice < 1 || choice  > 5)
  22.                 {
  23.                     System.out.print("That was an invalid choice -- enter 1 through 5 only");
  24.                     choice = keyboard.nextInt();
  25.                 }
  26.                 if(choice == choice1)
  27.                     person++;
  28.                     else 
  29.                         if(choice == choice2)
  30.                             person++;
  31.                                 else
  32.                                     if(choice == choice3)
  33.                                         person++;
  34.                                             else
  35.                                                 if(choice == choice4)
  36.                                                     person++;
  37.  
  38.         }
  39.         person=choice1+choice2+choice3+choice4;
  40.         perc1 = choice1/person;
  41.         perc2 = choice2/person;
  42.         perc3 = choice3/person;
  43.         perc4 = choice4/person; 
  44.  
  45.  
  46.         System.out.println("The total number of people surveyed: " + person);
  47.         System.out.println("THe results are as follows");
  48.         System.out.println();
  49.  
  50.         System.out.println(" Beverage                Number of Votes            Percentage");
  51.         System.out.println("-------------------------------------------------------------------------------------");
  52.         System.out.println();
  53.         System.out.println("Coffee                    "+choice1+ "                "+perc1);
  54.         System.out.println("Tea                                "+choice2+"                     "+perc2);
  55.         System.out.println("Orange Juice                 "+choice3+"                     "+perc3);
  56.         System.out.println("Lemonade                        "+choice4+"                     "+perc4);
  57.  
  58.     }
  59. }
Oct 19 '08 #1
3 1498
Dormilich
8,658 Expert Mod 8TB
ask one of the moderators to move it to the java forum.

regards
Oct 19 '08 #2
gits
5,390 Expert Mod 4TB
done ;)

kind regards
MOD
Oct 19 '08 #3
JosAH
11,448 Expert 8TB
@OP: if you want to squeeze all your (convoluted) logic in one single (main) method
you'll end up with a lot of zigzagging code that noone understands, including you.

Unravel your logic in to seperate small simple methods and decompose your
problem. Basically you want something like this:

Expand|Select|Wrap|Line Numbers
  1. int choice;
  2. while ((choice=getChoice()) != ENDCHOICE)
  3.    processChoice(choice);
  4. printSummary();
  5.  
The value for ENDCHOICE can be a final static private variable; it's up to you.
The getChoice() method only returns when/if the user has given a legal choice.
The processChoice() method only is passed a valid choice which is not equal
to ENDCHOICE; the logic of the while statement takes care of that,

The printSummary() method prints, *ahem*, a summary. You probably need a
few member variables that hold the data supplied by the user and calculated
by your class. All that data can be private data.

kind regards,

Jos
Oct 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Mike N. | last post by:
Hello: I have a form that contains a multiple-select field that has 12 options in it. I would like the user to be able to select UP TO FOUR of those options. If they select more than four, I...
21
by: cjl | last post by:
Hey all: I have the following (ugly) code: function preload() { if (cr_series.length >1) preloadCR(); if (ct_series.length >1) preloadCT(); if (mr_series.length >1) preloadMR(); if...
5
by: Melissa Cowan | last post by:
I am using Access 2000. I have the Developer's handbook and got the code for the mulit select listbox from there. It sends the selected value to another listbox, lstselected. What I need to do is...
9
by: terry | last post by:
I am a programmer (cobol, peoplesoft, sqr, etc.) so I am familiar with programming logic, etc. but not very familiar with C. I need a C program in a study I'm doing. The program is fairly simple,...
0
by: Kaur | last post by:
Hi, I am trying to create a crosstab type of report without using crosstab query. The report captures several survey questions and for each survey questions there are multiple choices that...
9
by: Divick | last post by:
Hi all, I have a problem related to std::string class. Is it ok to assign a global string variable to a local string object as shown below? I am trying to print the address of local string...
14
namcintosh
by: namcintosh | last post by:
Hello, everyone. Well, let me cut to the chase and explain my problem. I am trying to devise a menu plan that uses the if/else if and the while loop. The program calculates the user's weight...
0
by: chikhalepankaj | last post by:
Hi guys, I have a problem in reading a file content which r in tab separated value formate eg. MODULE AD0 AD1 AD2 AD3 AD4 AD5 AD6 AD7 4011 ...
3
by: jas2803 | last post by:
Hello, I have a table: The person gets to come back as often as possible and choose between 3 different items, A, B C, for the column. They can take as many as they want, column. I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.