Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Huge program, and I'm stuck and its frustrating me so (https://www.askmehelpdesk.com/showthread.php?t=151657)

  • Nov 13, 2007, 12:47 PM
    albear
    huge program, and I'm stuck and its frustrating me so
    right OK, it doesn't like the String winner = (whatever) bits at the bottom and I don't know why, please can anybody help me... PLEASE!!

    import java.util.*;
    class MyProblem
    {
    public static void main ( String [] args)



    {

    int [] theNums = new int [5];

    Scanner kybd = new Scanner(System.in);

    for (int I = 0; I < 5 ; ++i)
    {
    System.out.print("Enter your vote:");
    System.out.print("press 0 for 'candidate 1', 1 for 'candidate 2',");
    System.out.print(" 2 for 'candidate 3', 3 for 'candidate 4'and 4 for 'candidate 5' ");
    theNums[i] = kybd.nextInt();

    }
    int can0=0;
    int can1=0;
    int can2=0;
    int can3=0;
    int can4=0;


    for (int I = 0; I < 5 ; ++i)
    {

    if (theNums[i] == 0)
    {

    can0 += 1;
    }


    if (theNums[i]== 1)
    {

    can1 += 1;
    }


    if (theNums[i] == 2)
    {

    can2 += 1;
    }

    if (theNums[i] == 3)
    {

    can3 += 1;
    }

    if (theNums[i]== 4)
    {

    can4 += 1;
    }

    //if (theNums[i]==-1)
    //{
    //end;
    //}

    }


    if ((can0>can1)&&(can0>can2)&&(can0>can3)&&(can0>can4 ))
    {
    String winner = candidate1;
    }
    else if ((can1>can2)&&(can1>can3)&&(can1>can4))
    {
    String winner = candidate2;
    }
    else if ((can2>can3)&&(can2>can4))
    {
    String winner = candidate3;
    }
    else if (can3>can4)
    {
    String winner = candidate4;
    }
    else
    {
    String winner = candidate5;
    }



    System.out.println("winner is choice" + winner);


    }
    }
  • Nov 13, 2007, 12:53 PM
    albear
    1 Attachment(s)
    Well, this is what command prompt says.
  • Nov 13, 2007, 01:08 PM
    albear
    Please...
  • Nov 13, 2007, 03:43 PM
    albear
    Can anybody explain to me what it is I am forgetting/missing
  • Nov 14, 2007, 08:38 PM
    qcumber
    Hi albear,

    Hope I'm not too late.

    From your code, I'm assuming that candidate1 to candidate5 is a string and not some variables hidden somewhere. If it is, you will have to use


    if ((can0>can1)&&(can0>can2)&&(can0>can3)&&(can0>can4 ))
    {
    String winner = "candidate1";
    }
    else if ((can1>can2)&&(can1>can3)&&(can1>can4))
    {
    String winner = "candidate2";
    }
    else if ((can2>can3)&&(can2>can4))
    {
    String winner = "candidate3";
    }
    else if (can3>can4)
    {
    String winner = "candidate4";
    }
    else
    {
    String winner = "candidate5";
    }

    As for the variable winner, declare the winner String just below

    int [] theNums = new int [5];

    like

    int [] theNums = new int [5];
    Stirng winner = "";

    and then for all the String winner = ";

    and then for all the String winner = " lines... remove String.. e.g.. Winner = " lines ... remove String .. eg. winner = ".

    You should be good after this.
  • Nov 20, 2007, 02:44 PM
    albear
    Thank you q, I've been told to use a do-while so I'm going to change it and give that a shot, I've just completed a similar program, so hopefully ill be all right now,

  • All times are GMT -7. The time now is 12:08 AM.