help counting CHOICES

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nitric
    New Member
    • Sep 2008
    • 2

    help counting CHOICES

    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(pe rson who quits doesn't count) and counting each drink

    help?

    [CODE=java]import java.util.Scann er;
    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.prin tln("Enter choice for person# " + person);
    System.out.prin tln("1. Coffee 2. Tea 3. Orange Juice 4. Lemonade 5. Quit");
    choice = keyboard.nextIn t();
    if(choice < 1 || choice > 5)
    {
    System.out.prin t("That was an invalid choice -- enter 1 through 5 only");
    choice = keyboard.nextIn t();
    }
    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.prin tln("The total number of people surveyed: " + person);
    System.out.prin tln("THe results are as follows");
    System.out.prin tln();

    System.out.prin tln(" Beverage Number of Votes Percentage");
    System.out.prin tln("-------------------------------------------------------------------------------------");
    System.out.prin tln();
    System.out.prin tln("Coffee "+choice1+ " "+perc1);
    System.out.prin tln("Tea "+choice2+" "+perc2);
    System.out.prin tln("Orange Juice "+choice3+" "+perc3);
    System.out.prin tln("Lemonade "+choice4+" "+perc4);

    }
    }[/CODE]
    Last edited by gits; Oct 19 '08, 10:03 AM. Reason: fix code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    ask one of the moderators to move it to the java forum.

    regards

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      done ;)

      kind regards
      MOD

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        @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:

        Code:
        int choice;
        while ((choice=getChoice()) != ENDCHOICE)
           processChoice(choice);
        printSummary();
        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

        Comment

        Working...