I'm currently working on a java project, and im supposed to make a program that generates a number between 1000 and 2000, then gives you the chance to enter the amount of players, takes in all the names and guesses in two different arrays, then create two methods, one to find the closest guess to the actual numbers and another to report or find the winner match it up with their guess and print it out. Im having trouble with my second method, im drawing a blank on the logic part right now this is my code and output:
public static void reportWinner (int answer, int winner, int [] array, String [] array1)
{
ArrayList tempList = new ArrayList();
ArrayList tempList1 = new ArrayList();
int counter = 0;
int temp = 0;
for(int index = 0; index < array.length; index++)
{
temp = answer - array[index];
temp = Math.abs(temp);
if(temp == winner)
{
counter++;
tempList.add(array1[index]);
tempList1.add(array[index]);
}
}
for(int index = 0; index < tempList.size(); index++)
{
if(tempList.size() == 1);
{
System.out.println("The winner is " + tempList.get(0) + ", with a guess of " + tempList1.get(0) + " which is "
+ winner + " away from the actual number of jelly beans.");
if(tempList.size() > 1)
{
System.out.println("The winners are: ");
System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is "
+ winner + " away from the actual number of jelly beans.");
}
}
if(tempList.size() == 1 && winner == 0)
{
System.out.println("The winner is " + tempList.get(0) + ", with a guess of " + tempList1.get(0) + " which is "
+ "the exact number of jelly beans in the jar.");
if(tempList.size() > 1)
{
System.out.println("The winners are: ");
System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is "
+ "the exact number of jelly beans in the jar.");
}
}
}
}
This is the output it is producing when there is more than one winner.
There were 1532 jelly beans in the jar. The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans. The winners are: Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans. The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans. The winners are: greg, with a guess of 1200 which is 332 away from the actual number of jelly beans. The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans. The winners are: Jim, with a guess of 1200 which is 332 away from the actual number of jelly beans.
This is what it should look like
There were 1500 jelly beans in the jar.
The winners are:
Mike with a guess of 1475, which is 25 away from the actual number of jelly beans. Tony with a guess of 1525, which is 25 away from the actual number of jelly beans.
Im having trouble with the method portion i know this much but if you need to see the rest of the code let me know any help would be appreciated, thanks. (I think i need a way to void the first print out if the counter passes one but im not sure how to go about this)
Aucun commentaire:
Enregistrer un commentaire