> I need help with this java program?

I need help with this java program?

Posted at: 2014-12-18 
You can make the game replay by making a seperate method for the Guessing game code and calling it at the end once the user guesses the right number.

EDIT: It generates a crazy random number because you are not generating a number between 0 and max. Read the second comment on http://stackoverflow.com/questions/36368... to figure out how, I've modified your code with how to implement that.

EDIT2: I see where you went wrong, Your if statements. Remember that MAX is not the number the user is trying to guess, it's num. I've modified the code, try it out now.

package tester;

import java.util.Random;

import java.util.Scanner;

public class tester {



public tester() {

guess();

}





public static void guess() {

Scanner keyboard = new Scanner(System.in);

Random gen=new Random(); //using system clock to seed

int tries = 0;

int max;

int guess = 0;

int num = 0;

System.out.print("You will be trying to guess a number between certain certain values\n");

System.out.print("Enter the maximum number in the range: ");

max=keyboard.nextInt();



System.out.println("I am thinking of a number between 0 and "+max);

num = gen.nextInt((max-0) + 1 ) - 0;



while (guess != num)

{

System.out.print("Enter a number: ");

guess = keyboard.nextInt();

tries++;

if(guess == -999)

{

System.out.print("The number is: ");

System.out.print(num);

System.exit(0);

}else if(guess>max||guess<0)

{

System.out.println("You need to use a number that's between 0 & "+max);

tries--;

}else if (guess > num)

{

System.out.println("Too high! Try again...\n");

} else if (guess < num)

{

System.out.println("Too low! Try again...\n");

}

else break;

}

System.out.println("You WIN!!! It took you "+ tries + " tries!");

System.out.println();

System.out.println();

guess();

}

public static void main(String[] args) {

new tester();

}

}

I need to make a program that's a game of hi/lo where the user has to guess the number between certain values.I can get the program to run properly but I can't get it to start all over when the user guesses the value right.& when the user enters "-999" as a guess its supposed to reveal the answer but when I enter "-999" It gives me a crazy number...I need the loop to start over again & I need the previously generated random number to be shown when "-999" is entered

Eddie its supposed to generate the random number that the user is supposed to guess..

import java.util.Random;

import java.util.Scanner;

public class HiLo

{

public static void main(String args[])

{

Scanner keyboard = new Scanner(System.in);

Random gen=new Random(); //using system clock to seed

int tries = 0;

int max;

int guess = 0;

int num;

System.out.print("You will be trying to guess a number between certain certain values\n");

System.out.print("Enter the maximum number in the range: ");

max=keyboard.nextInt();

System.out.println("I am thinking of a number between 0 and "+max);

for(int count=0;count>max;)

num=gen.nextInt();

while (guess != max)

{

System.out.print("Enter a number: ");

guess = keyboard.nextInt();

tries++;

if(guess==-999)

{

System.out.print("The number is: ");

System.out.print(gen.nextInt());

System.exit(0);

}else if(guess>max||guess<0)

{

System.out.println("You need to use a number that's between 0 & "+max);

tries--;

}else if (guess > max)

{

System.out.println("Too high! Try again...\n");

} else if (guess < max)

{

System.out.println("Too low! Try again...\n");

}

}

System.out.println("You WIN!!! It took you "+ tries + " tries!");

}

}

put a ; at the end.