Results 1 to 12 of 12
Thread: First time with loops
- 02-19-2013, 05:25 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
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.
When this is run....in a perfect world...if guess == roll then congrats. if not...sorry...if its <1 || >6 ...it doesn't exist.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; } }
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?
- 02-19-2013, 05:46 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 02-19-2013, 06:03 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 682
- Rep Power
- 1
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.
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 06:31 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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 ...When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-19-2013, 06:44 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
Re: First time with loops
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?
- 02-19-2013, 06:59 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 682
- Rep Power
- 1
Re: First time with loops
Your loop should go just after
But then you will have some redundant code which you need to clean up and fix.Java Code:int score = 0;
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 07:07 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
Re: First time with loops
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.
So I have that firstJava 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; } }
to lead into the loop.Java Code:System.out.println("Pick a number between " + a + " and " + b + ":"); guess = scan.nextInt();
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
- 02-19-2013, 07:12 PM #8
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
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
- 02-19-2013, 07:17 PM #9
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 682
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 08:18 PM #10
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
Re: First time with loops
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
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?Java Code:for (guesses = 0; guesses < maxGuesses; guesses++)
- 02-19-2013, 08:30 PM #11
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 682
- Rep Power
- 1
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.
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 09:03 PM #12
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
enter the time zone offset to GMT and convert to current time
By Bonia in forum New To JavaReplies: 5Last Post: 02-29-2012, 10:07 AM -
Desingn time properties missing at run time - Netbeans
By junaid in forum New To JavaReplies: 3Last Post: 12-07-2011, 07:00 AM -
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks