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?
Code:
while not correct and guesses < maxGuesses
prompt user for guess.
check guess.
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
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 ...
Re: First time with loops
Quote:
Originally Posted by
Tolls
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?
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?
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.
Regards,
Jim
Re: First time with loops
Quote:
Originally Posted by
jim829
Your loop should go just after
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.
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
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?
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
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
Re: First time with loops
Quote:
Originally Posted by
jim829
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
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?
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
Re: First time with loops
Thanks a lot Jim, I figured it out. pretty cool stuff, I'm enjoying this Java stuff!