Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Please what is wrong with my code

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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();
    }
    }


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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:

    int number = guessNumber();
     
    while(player != number)
    {
    ...

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Location
    U.S.A.
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    your code here
    .

    I hope we can help with your problem.

Similar Threads

  1. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 29th, 2012, 03:33 PM
  2. what is wrong with this code???plz help me :(
    By kanikapant in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 17th, 2011, 06:03 PM
  3. want wrong with my code
    By drum in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 09:06 AM
  4. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM
  5. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 03:38 AM