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
I am trying to develop a small apps that will enable someone guess a random number and the program will reply if the number is right or woring
Re: Please what is wrong with my code
Does the above code work? if not what is the problem and/or error message. also if you put it in [CODE] tags in your post it makes it a lot easier for people to read it and spot problems.
Re: Please what is wrong with my code
Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Re: Please what is wrong with my code
actually you are generating a new random number each time you ask the user for input.
1. generate a random number once. store it in a variable, and afterwards compare it afterwards
2. also the random function is also not working properly.
3. put the code inside while loop
so might look something like this :
...
Re: Please what is wrong with my code
bhatt.umang7, welcome to the forums. Please read the forum rules, and I highly recommend reading the following: http://www.javaprogrammingforums.com...n-feeding.html