Ask Experts Questions for FREE Help!
 

Free Answers in 3 Easy Steps

Register Now
3 Steps
 


Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.
  View Answers    Answer this question    Ask a question  
 

albear
Nov 13, 2007, 11:47 AM
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, 11:53 AM
Well, this is what command prompt says.

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

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

qcumber
Nov 14, 2007, 07: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 = "candidate1" lines ... Remove String .. E.g.. Winner = "candidate1".

You should be good after this.

albear
Nov 20, 2007, 01: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 alright now,