Please what is wrong with my code
import java.util.Random;
import java.util.Scanner;
public class Ex6Qus33
{
Random randomNumbers = new Random(10);
public int guessNumber()
{
int numberGuess = 1 + randomNumbers.nextInt(1000);
return numberGuess;
}
public void playNumber()
{
String a = "Yes";
String b = "No";
randomNumbers.setSeed(10);
Scanner input = new Scanner(System.in);
System.out.print("Guess a number between 1 and 1000:- ");
int player = input.nextInt();
guessNumber();
while(player != guessNumber())
{
if(player < guessNumber())
{
System.out.println("Too Low. Try again!!!");
System.out.print("Guess a number between 1 and 1000:- ");
player = input.nextInt();
}
else
if(player > guessNumber())
{
System.out.println("Too High. Try again!!!");
System.out.print("Guess a number between 1 and 1000:- ");
player = input.nextInt();
}
}
System.out.println("Congratulations. You guessed the number.!!!");
System.out.println("Do wish to play again?");
String play = input.nextLine();
if(play.equals(a))
guessNumber();
else
if(play.equals(b))
System.out.println("End of Game");
}
public static void main(String args[])
{
Ex6Qus33 apps = new Ex6Qus33();
apps.playNumber();
}
}
Re: Please what is wrong with my code
You should not call guessNumber() more then once. Every time you call guessNumber() you get a new number.
Instead you will save the the value in an variable and use it. Like:
Code :
int number = guessNumber();
while(player != number)
{
...
Re: Please what is wrong with my code
LordDavid, I would recommend asking a question and describing what is wrong. It seems some people can guess, but only you know if that guess is correct without providing a description of the problem.
Re: Please what is wrong with my code
Yeah, LordDavid, please tell us what is happening when you run your code. Also, please wrap the code tag around your code. .
I hope we can help with your problem.