PDA

View Full Version : Huge program, and I'm stuck and its frustrating me so


albear
Nov 13, 2007, 12:47 PM
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);


}
}

albear
Nov 13, 2007, 12:53 PM
Well, this is what command prompt says.

albear
Nov 13, 2007, 01:08 PM
Please...

albear
Nov 13, 2007, 03:43 PM
Can anybody explain to me what it is I am forgetting/missing

qcumber
Nov 14, 2007, 08:38 PM
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.

albear
Nov 20, 2007, 02:44 PM
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,