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 6 of 6

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
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

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

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


    ...
    Last edited by copeg; April 29th, 2012 at 03:32 PM.

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

    Default 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

Similar Threads

  1. What's Wrong With My Code?
    By Nuggets in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 31st, 2012, 10:11 PM
  2. What is the wrong with this code?
    By Subhasri in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2011, 08:14 AM
  3. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 05:30 PM
  4. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM