Results 1 to 12 of 12
  1. #1
    johnWeldon is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default First time with loops

    So i'm doing a class assignment and it's the first time working with loops. Now when my code is without the "while" loop, it works correctly, but after adding it in...it doesn't give the expected results. Obviously a mistake on my part since I'm new to Java.

    So I had to make a rolling a die game...pick a number 1 - 6. I'm not entirely sure what's wrong here.

    Java Code:
    package egr118;
    import java.util.*;
    public class Lab2 {
    
        public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int a = 1;
        int b = 6;
        int guess;
        int roll = getRandomIntInRange(a,b);
        int guesses = 0;
        int maxGuesses = 10;
        int score = 0;
        
        System.out.println("Pick a number between " + a + " and " + b + ":");
        guess = scan.nextInt();
        
        boolean correct = (guess == roll);
        
        if (guess <a || guess >b )
        {
            System.out.println(guess + " does not exist! Your score is now " + score--);
        }
        
         if (correct)
        {
            System.out.println("You picked " + guess + " Great guess! Your score"
                    + " is now " + score++);
        }
        else if(guess != roll)
        {
            System.out.println("You picked " + guess + " Sorry the roll was " + roll
                    + " Your score is now " + score--);
        }
        
       
        while (guesses < maxGuesses)
        {
            guesses++;
            
            correct = (guess == roll);
            roll = getRandomIntInRange(a,b);
            a = 1;
            b = 6;
            
            System.out.println("Pick a number between " + a + " and " + b + ":");
        guess = scan.nextInt();
        
        
        if (guess <a || guess >b )
        {
            System.out.println(guess + " does not exist! Your score is now " + score--);
        }
        
        if (correct)
        {
            System.out.println("You picked " + guess + " Great guess! Your score"
                    + " is now " + score++);
        }
        else if (guess != roll)
        {
             System.out.println("You picked " + guess + " Sorry the roll was " + roll
                    + " Your score is now " + score--);
        }
      }
        System.out.println("Your final score is: " + score);
    }
        public static int getRandomIntInRange(int a , int b)
        {
            return (int)((b - a + 1) * Math.random()) + a;
        }
        
    }
    When this is run....in a perfect world...if guess == roll then congrats. if not...sorry...if its <1 || >6 ...it doesn't exist.

    But when run...the output looks like:

    Pick a number between 1 and 6:
    1
    You picked 1 Great guess! Your score is now 0
    Pick a number between 1 and 6:
    4
    You picked 4 Great guess! Your score is now 1
    Pick a number between 1 and 6:
    2
    You picked 2 Sorry the roll was 5 Your score is now 2
    Pick a number between 1 and 6:
    5
    You picked 5 Sorry the roll was 6 Your score is now 1
    Pick a number between 1 and 6:
    1
    You picked 1 Sorry the roll was 3 Your score is now 0
    Pick a number between 1 and 6:
    5
    You picked 5 Sorry the roll was 2 Your score is now -1
    Pick a number between 1 and 6:
    2
    You picked 2 Sorry the roll was 5 Your score is now -2
    Pick a number between 1 and 6:
    6
    Pick a number between 1 and 6:
    6
    You picked 6 Great guess! Your score is now -3
    Pick a number between 1 and 6:
    7
    7 does not exist! Your score is now -2
    You picked 7 Sorry the roll was 3 Your score is now -3
    Pick a number between 1 and 6:
    8
    8 does not exist! Your score is now -4
    You picked 8 Sorry the roll was 6 Your score is now -5
    Your final score is: -6


    Can anyone help point me to my mistakes here?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: First time with loops

    I'm not too sure what you;re trying to do, but shouldn't all your guess code be inside the loop?
    I don't qute understand why you have some of it outside the loop?

    Java Code:
    while not correct and guesses < maxGuesses
       prompt user for guess.
       check guess.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: First time with loops

    You need to move your while statement to include all your code. You will then have duplicate code in the loop which is not necessary.

    Also the scores are printing out incorrectly. If you post-decrement or post-increment a value, (e.g. val++) it is first referenced (or in your case printed) and then the value is adjusted. If you pre-decrement or pre-increment (e.g. ++val) the value is referenced after it is adjusted. I believe you want the latter.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: First time with loops

    Maybe you don't like to see those old scores; if so, change score++ and score-- to (++score) and (--score).

    kind regards,

    Jos

    edit: too slow again ...
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    johnWeldon is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: First time with loops

    Quote Originally Posted by Tolls View Post
    I'm not too sure what you;re trying to do, but shouldn't all your guess code be inside the loop?
    I don't qute understand why you have some of it outside the loop?

    Java Code:
    while not correct and guesses < maxGuesses
       prompt user for guess.
       check guess.
    This is the way my prof showed us.

    So what you're saying is that all the variables and EVERYTHING should be in the loop?

    I thought the variables would be declared outside of the loop and then the code is put in the loop.

    See what my prof has on this paper he handed out is... after writing the code...make a "for" loop (i'm attempting it with while because I'm not too sure with "for" loops)

    So it should be all the variables declared before the loop is made. When the loop is made, ALL the code goes inside it?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: First time with loops

    Your loop should go just after
    Java Code:
    int score = 0;
    But then you will have some redundant code which you need to clean up and fix.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    johnWeldon is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: First time with loops

    Quote Originally Posted by jim829 View Post
    Your loop should go just after
    Java Code:
    int score = 0;
    But then you will have some redundant code which you need to clean up and fix.

    Regards,
    Jim
    Thanks Jim.

    Okay so now (i'm going to switch to the "for loop" for now to see what it'll be like for me.

    Java Code:
    package egr118;
    import java.util.*;
    public class Lab2 {
    
        public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int a = 1;
        int b = 6;
        int guess;
        int roll = getRandomIntInRange(a,b);
        int guesses;
        int maxGuesses = 10;
        int score = 0;
       
        System.out.println("Pick a number between " + a + " and " + b + ":");
        guess = scan.nextInt();
        
        
        for (guesses = 0 ; guesses < maxGuesses; guesses++)
        {
            
            boolean correct = (guess == roll);
            roll = getRandomIntInRange(a,b);
            a = 1;
            b = 6;
            
            System.out.println("Pick a number between " + a + " and " + b + ":");
        guess = scan.nextInt();
        
        if (correct)
        {
            System.out.println("You picked " + guess + " Great guess! Your score"
                    + " is now " + score++);
        }
        else if (guess <a || guess >b )
        {
            System.out.println(guess + " does not exist! Your score is now " + score--);
        }    
    
        else
        {
             System.out.println("You picked " + guess + " Sorry the roll was " + roll
                    + " Your score is now " + score--);
        }
      }
        System.out.println("Your final score is: " + score);
    }
        public static int getRandomIntInRange(int a , int b)
        {
            return (int)((b - a + 1) * Math.random()) + a;
        }
        
    }
    So I have that first

    Java Code:
    System.out.println("Pick a number between " + a + " and " + b + ":");
        guess = scan.nextInt();
    to lead into the loop.

    So now...Here's my output

    Pick a number between 1 and 6:
    5
    Pick a number between 1 and 6:
    6
    You picked 6 Great guess! Your score is now 0
    Pick a number between 1 and 6:
    3
    You picked 3 Sorry the roll was 1 Your score is now 1
    Pick a number between 1 and 6:
    4
    You picked 4 Sorry the roll was 4 Your score is now 0 Still saying guess != roll :/
    Pick a number between 1 and 6:
    1
    You picked 1 Great guess! Your score is now -1
    Pick a number between 1 and 6:
    5
    You picked 5 Great guess! Your score is now 0
    Pick a number between 1 and 6:
    6
    You picked 6 Sorry the roll was 4 Your score is now 1
    Pick a number between 1 and 6:
    9
    9 does not exist! Your score is now 0
    Pick a number between 1 and 6:
    3
    You picked 3 Sorry the roll was 1 Your score is now -1
    Pick a number between 1 and 6:
    4
    You picked 4 Sorry the roll was 3 Your score is now -2
    Pick a number between 1 and 6:
    2
    You picked 2 Sorry the roll was 3 Your score is now -3
    Your final score is: -4

    This is why I had put the "if" statements and everything before the loop as well as in the loop, because for this first output...How can I have my loop happen already?

    So is there a way for the loop to happen (before anything else as you guys have pointed out) but still ask for that first number, scan the input, and do the condition that it meets?
    Last edited by johnWeldon; 02-19-2013 at 07:09 PM. Reason: wrong

  8. #8
    johnWeldon is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: First time with loops

    Nvm, i just moved the first output statement up to the beginning of the loop so it shows up now...but I'm still having the problem with it saying guess != roll

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: First time with loops

    I would do the following:

    • Initialize all local variables like you have done
    • Create the scanner for console input
    • Then place the while loop checking on number of allowable guesses
      • generate dice throw
      • prompt for user input
      • do appropriate checks, update score, and inform player of results

    • End loop
    • close scanner


    Hope this helps.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    johnWeldon is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: First time with loops

    Quote Originally Posted by jim829 View Post
    I would do the following:

    • Initialize all local variables like you have done
    • Create the scanner for console input
    • Then place the while loop checking on number of allowable guesses
      • generate dice throw
      • prompt for user input
      • do appropriate checks, update score, and inform player of results

    • End loop
    • close scanner


    Hope this helps.

    Regards,
    Jim
    Got it! Thanks a lot Jim!

    Now the next part says, create a new loop that chooses 2 random numbers between 1 and 10 and compare them. With 10, 100, 1000 guesses and then print out the percentage. They were correct

    I'm going to use the "for" loop again.

    So i'm setting it up like

    Java Code:
    for (guesses = 0; guesses < maxGuesses; guesses++)
    As far as that, how do I make it so they choose 2 numbers and compare them? When I did the 1 number, I just had to get an input of 1 number...just any kind of advice here?

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: First time with loops

    Instead of user input it sounds to me like you just generate another random number within the specified limits and see how often the two match.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    johnWeldon is offline Member
    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    Default Re: First time with loops

    Thanks a lot Jim, I figured it out. pretty cool stuff, I'm enjoying this Java stuff!

Similar Threads

  1. Replies: 5
    Last Post: 02-29-2012, 10:07 AM
  2. Replies: 3
    Last Post: 12-07-2011, 07:00 AM
  3. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  4. Replies: 20
    Last Post: 03-22-2011, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •