# Guessing game help

Printable View

• March 11th, 2012, 08:26 PM
np657
Guessing game help
Hi guys, so I am trying to do a program that takes a limited number of users 2-5 and then gets there guesses and records them till they select the correct one. The winner is determined by whoever makes the least amount of guesses for there random number. So far here is my work, but I am stuck on the part for getting the amount of guesses and finding out the winner. Any help?

public static void main(String[] args) {

System.out.println("How many players between 2-5?"); // this collects the amount of players in the game using method recNum
int u = recNum();

int [ ] players = new int [ u ]; //this array holds the number guessed by each player using the method get recNum

for (int i = 0; i < players.length; i++ )
{
System.out.println("Please enter your guess now player" + i);
players[i]=recNum();
}

int [] guesses = new int [u ]; //this array holds the random number using the method getNum

for (int c = 0; c < guesses.length; c++)
{
guesses[c]=getNum();

}

int []z;
boolean f = true;
do{

z = compare(players, guesses, u);
} while (!f);

winner(z);

}

public static void winner(int z[])
{

int leader = 0;
for (int i = 0; i < z.length; i++)
{
if (z[i] > leader)
{
leader = z[i];
}
}
System.out.println("The winner is player" + leader);
}
public static int[] compare(int players[], int guesses[], int u)
{
int [] x = new int[u];
boolean zebra = true;
while (zebra){
for(int q = 0; q < x.length; q++)
{

if (players[q] < guesses[q])
{
System.out.println("Player" + " " + q + " your guess is too low");
System.out.println(guesses[q]);
x[q]++;

players[q]=recNum();

}
else if (players[q] > guesses[q])
{
System.out.println("Player" + " " + q + " your guess is too high");
System.out.println(guesses[q]);
x[q]++;

players[q]=recNum();

}
else
{

System.out.println("Player" + " " + q + " your guess is correct");
System.out.println(guesses[q]);
zebra = false;
x[q]++;

}

}

}

return x;

}

public static int recNum()
{
Scanner scanz = new Scanner(System.in);
int n1 = 0;
boolean guess = false;
while(!guess){
try{
n1 = scanz.nextInt();
}
catch(InputMismatchException inputMismatchException)
{

System.out.println("Your entry was not an integer, please try again");

continue;
}
if(n1>100 || n1<1){

System.out.println("Your guess must be between 1 and 100 please try again");

continue;
}
else{
guess = true;
}
}
return n1;
}

public static int getNum(){ //this method goes and creates a random number from 1-100 using the imported java ulti random

Random geney = new Random();
int n2 = 0;
int min = 1;
int max = 100;
n2 = geney.nextInt(max - min + 1) + min;
return n2;
}
• March 12th, 2012, 07:39 AM
KevinWorkman
Re: Guessing game help
When posting code, please use the highlight tags. Unformatted code is very hard to read.

Take your problems one at a time. How do you know when a user has won? When should you check for that? How do you know when a user makes a guess? How would you keep track of that?