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

Thread: guessing game

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default guessing game

    im trying to generate a random number and have the user guess it until they get it right here is the code:



    import java.util.Scanner;
    import java.util.Random;
    public class Welcome
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);

    int number;
    number = GetNumber(5);

    boolean win = false;

    int guess;
    guess = GetGuess(5);

    while( win = false)
    {
    if( guess > number)
    {
    System.out.print("Too high. Try Again");
    guess = GetGuess(5);
    }

    if( guess < number)
    {
    System.out.print("Too low. Try Again");
    guess = GetGuess(5);
    }
    }

    if( guess == number)
    {
    win = true;
    System.out.print("Congratulations you win");
    }
    }

    public static int GetGuess(int guess)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Guess a number between 1 and 100: ");
    guess = input.nextInt();
    return guess;
    }
    public static int GetNumber(int number)
    {
    Random rand = new Random();
    number = rand.nextInt(100) + 1;
    return number;
    }
    }

    and this is the output im getting:

    Guess a number between 1 and 100: 67
    Press any key to continue . . .


    I am just a beginner at using java programming and i am using Textpad if that makes any difference at all.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: guessing game

    Hint: == != =.

    When posting code, make sure you follow standard naming conventions, and don't forget the highlight tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. guessing game assignment
    By scottey313 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 22nd, 2011, 07:52 PM
  2. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM
  3. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  4. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM
  5. Guessing Game begginner question
    By okupa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 07:31 AM