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

Thread: Fixing of bug in number guessing game

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Fixing of bug in number guessing game

    hey everyone i am stuck with this program. im trying to make a number guessing game using loops and if statements. i know how to do them individually but i am struggling with doing them together. heres what i have so far and useful feedback is greatly appreciated. oh yeah its all supposed to be in the main.

    import java. util .Scanner;
    import java.util.*;
     
    public class NumberGame
    {
        public static void main(String[] args)
        {
           Random random = new Random();
           int rand = random.nextInt(1000);
     
            for(rand=1000; rand<=1000; rand++)
            {
            System.out.println("please enter a number between 1 and 1000");
            if(guess<=500); 
     
            }
     
    private int guess;
        }
    }

    im not sure if i have the for loop set up right but the part thats got me all confused is the if statement.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help with this program (loops and if statements togethor)

    What input/output are you looking for? Please give me an example.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help with this program (loops and if statements togethor)

    like if the random number is 559 and i put in 340 it should say something like your guess is to low try guessing between 341 and 560.

  4. #4
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help with this program (loops and if statements togethor)

    i think i figured one thing out i have to do a nested loop with in order to have guess loop until the answer is right.

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help with this program (loops and if statements togethor)

    How about this?

    import java.util.Scanner;
    import java.util.*;
     
    public class NumberGame {
     
        public static void main(String[] args) {
     
            Scanner sc = new Scanner(System.in);
     
            Random random = new Random();
            int rand = random.nextInt(100);
     
            int guesses = 0;
     
            System.out.println(rand);
     
            System.out
                    .println("I'm thinking of a number between 0 and 100. Whats your guess?");
     
            while (sc.hasNextInt()) {
                int myGuess = sc.nextInt();
                if (myGuess == rand) {
                    guesses++;
                    System.out.println("Congratulations! I was thinking " + rand
                            + ". It took you " + guesses + " guesses.");
                    break;
                } else if (myGuess < rand) {
                    System.out.println("You need to guess higher");
                    guesses++;
                } else if (myGuess > rand) {
                    System.out.println("You need to guess lower");
                    guesses++;
                }
            }
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. The Following User Says Thank You to JavaPF For This Useful Post:

    big_c (April 15th, 2009)

  7. #6
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help with this program (loops and if statements togethor)

    sweet its just what i was looking for. im really good with the while loop idk why i just didnt stick with that in the first place

  8. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Cool Re: help with this program (loops and if statements togethor)

    Quote Originally Posted by big_c View Post
    sweet its just what i was looking for. im really good with the while loop idk why i just didnt stick with that in the first place
    Glad I could help.

    Its all trial and error when you first start programming. You need to try different methods to find out what works and what doesn't. Soon you will know the best route to take
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM