# Thread: Having difficulty executing a series of loops in program

1. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## Having difficulty executing a series of loops in program

I started learning programming about a month ago and am writing a MasterMind program which allows the user to guess a 3-digit, randomly-generated number. I believe I've figured out most of the program, but am stuck on the final aspect of the program's logic.

For each guess, I want to first check if the solution is correct; if so, then the program should ask the user if he or she wants to play again. If a guess is incorrect, the program should display the number of correctly-positioned digits in his or her guess and the sum of those same correctly-positioned digits. It should then allow them to continue guessing until they have reached the maximum number of allowed guesses, which are predetermined according to their selected difficulty level.

Java Code:
```import java.util.Scanner;
import java.lang.Math;

/**	Import the Scanner and Math classes from the Java library.*/

public class MasterMind {

public static void main(String[] args) {
int user_guesses, max_guesses, game_level, guess;
double balance = 0.0, bet;
final double min_bet = 2.0;
String guessString;
Scanner keyboard = new Scanner(System.in);

int numCorrect = 0;	// 	number of digits guessed correctly
int incorrect = 0;	// 	number of incorrect guesses
int sessionSum = 0; // 	sum of correctly positioned guesses per session

System.out.print("\nWould you like to play Mastermind (y/n)? ");
String user_response = keyboard.nextLine();
//	User is prompted to declare whether he or she wishes to play
//	the MasterMind game.

if( user_response.toLowerCase().charAt(0) == 'y' ) {
System.out.print("\nEnter the amount of money you currently have: ");
balance = keyboard.nextDouble();
//	If the users input String, after being converted to lower case, is
// 	equal to "yes", then the user is asked to input his or her current
//	balance. Program reads the next Double from the user's keyboard.
//	This input, if valid, is assigned to var "balance".

while ( balance < 2.00 ) {
System.out.println("\nSorry, you must have at least 2.00 dollars to play MasterMind.\nGoodbye.\n");
System.exit(0);
}
/**	Error-checking while loop handles invalid input for the balance variable.*/

//	MasterMind requires a \$2.00 minimum balance. If the user enters a balance less
//	than the minimum, a message is displayed to the user to alert him or her of
//	this requirement; subsequently, the program exits.
}
// 	Ends if loop.

else {
System.out.println("\nGoodbye, see you next time.");
System.exit(0);
}
//	If the user enters indicates he or she doesn't want
//	to play, the program displays the above and exits.

// NOTE: consider replacing with isEqual for String input "yes" | "no"

while( user_response.toLowerCase().charAt(0) == 'y') {
System.out.print("\nPlease enter difficulty level. Enter 1 for beginner, 2 for intermediate, or 3 for advanced: ");

while (true) {
game_level = keyboard.nextInt();
if (game_level == 1 || game_level == 2 || game_level == 3) break;
System.out.println("\nSorry, but entry is invalid.");
System.out.print("Please enter valid difficulty level. Enter 1 for beginner, 2 for intermediate, or 3 for advanced: ");
}
/**	Receive user's difficulty level using error-checking while loop.*/

// 	Program receives using user input via the "nextInt()"
//	method (from the "Scanner" class). If this input is
//	equal to 1, 2, or 3, the program breaks from the loop;
//	otherwise, the program loops and prompts the user to
//	enter a valid integer.

System.out.print("\nEnter the amount you want to bet: ");
while (true) {
bet = keyboard.nextDouble();
if (bet <= balance) break;
System.out.println("\nSorry, but you cannot bet more money than you have.");
System.out.print("Enter the amount you want to bet: ");
}
/**	Receive user's bet using error-checking while loop.*/

// 	Program receives using user input via the "nextInt()"
//	method (from the "Scanner" class). If this input is
//	equal to 1, 2, or 3, the program breaks from the loop;
//	otherwise, the program loops and prompts the user to
//	enter a valid integer.

int random = randomNumber(game_level);
//	uses method randomNumber

String randomString = String.valueOf(random);
int secretCodeDigits = String.valueOf(random).length(); //	calc length

int r1 = random / 100;					//  hundred's place
int r2 = random % 100 / 10;				//	ten's place
int r3 = random % 100 % 10 / 1;			// 	one's place

System.out.println("\n< CHEAT MODE IS ENABLED. > The answer is: " + random +"."); // ad hoc CHEAT MODE
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");

guess = keyboard.nextInt();
guessString = String.valueOf(guess);

int g1 = guess / 100;				// 	hundred's digit
int g2 = guess % 100 / 10;			// 	ten's digit
int g3 = guess % 100 % 10 / 1;		// 	one's digit

System.out.println(g1 + g2 + g3 + " is your guess."); 	// checkpoint

max_guesses = gameGuesses(game_level);
user_guesses = 0;
int guessesRem = max_guesses; // number of guesses user has left

while( user_guesses < max_guesses )
{

if ((r1 == g1) & (r2 == g2) & (r3 == g3))
{
System.out.println("You won!!!");
System.out.println("You had " + incorrect + "wrong guesses.");
System.out.println("You earned: ");
System.out.println("Your balance is now x dollars.");
}
else if ((r1 == g1) & (r2 == g2))
{
numCorrect = 2;
sessionSum = r1 + r2;
user_guesses++;
guessesRem--;
incorrect++;
System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
}
else if ((r1 == g1) & (r3 == g3))
{
numCorrect = 2;
sessionSum = r1 + r3;
user_guesses++;
guessesRem--;
incorrect++;
System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();

}
else if ((r2 == g2) & (r3 == g3))
{
numCorrect = 2;
sessionSum = r2 + r3;
user_guesses++;
guessesRem--;
incorrect++;
System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
}
else if (r1 == g1)
{
numCorrect = 1;
sessionSum = r1;
user_guesses++;
guessesRem--;
incorrect++;
System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
}
else if (r2 == g2)
{
numCorrect = 1;
sessionSum = r2;
user_guesses++;
guessesRem--;
incorrect++;
System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
}
else if (r3 == g3)
{
numCorrect = 1;
sessionSum = r3;
user_guesses++;
guessesRem--;
incorrect++;
System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
}
else
{
numCorrect = 0;
sessionSum = 0;
user_guesses++;
guessesRem--;
incorrect++;
System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
}

}
}

System.out.println("You have won: " + balance);

} //  ++++  E N D   M A I N  +++++

/* 	gameGuesses method assigns the maximum number
of guesses permitted according to the user's
chosen level of difficulty.
*/
public static int gameGuesses( int game_level ) {
switch( game_level ){
case 1: return 30;
case 2: return 20;
case 3: return 10;
}
return 0;
//	else return 0
}

/* randomNumber method generates random number */
public static int randomNumber(int game_level)
{
int secretCode;

if( game_level == 1  || game_level == 2 || game_level == 3) {
secretCode = (int)( Math.random() * 900 ) + 100;
return secretCode;
}
//	If the game_level input is valid (i.e., 1, 2, or 3),
//	multiply that input by the product of a randomly-
//	generated number and 900, and add 100 to its result.
//	Lastly, assign this number to the var "secretCode"
return 0;
// 	else return 0
}

}```
When I run the code, it works all the way through the first incorrect guess...

Java Code:
```Would you like to play Mastermind (y/n)? y

Enter the amount of money you currently have: 55

Please enter difficulty level. Enter 1 for beginner, 2 for intermediate, or 3 for advanced: 3

Enter the amount you want to bet: 55

< CHEAT MODE IS ENABLED. > The answer is: 499.

Please guess the 3-digit number: 433

You have 9 guesses reamining.
Number of correct digits: 1
Sum: 4

Please guess the 3-digit number: 493

You have 8 guesses reamining.
Number of correct digits: 1
Sum: 4

Please guess the 3-digit number: 499

You have 7 guesses reamining.
Number of correct digits: 1
Sum: 4```
Could someone help me understand what's wrong and why?
Last edited by serenityN0W; 07-27-2011 at 09:41 PM.  Reply With Quote

2. ## it works all the way through the first incorrect guess...
Can you explain what is not working?

Have you tried debugging your code by adding println statements to show the values of variables as they change and to show the execution flow.
What variable controls the continuation of the execution? Print it out every time it changes:
System.out.println("var=" + var);  Reply With Quote

3. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## I have to run out for a bit, but I'll answer your first question for now...

It seems that the
Java Code:
`while( user_guesses < max_guesses )`
loop works fine, but only once. For example, suppose the correct answer is 953. I enter in "952". The program then tells me I have 2 correct digits (i.e., "9" and "5") whose sum is "14". I am then prompted to guess again. This time I guess "953", the correct answer, but still receive the same output as the first try (i.e., 2 correct digits, sum = 14). If I then enter "428", same output.

I believe the problem lies somewhere in the combination of loops, but can't seem to figure out where I've gone wrong.  Reply With Quote

4. ## while( user_guesses < max_guesses )
loop works fine, but only once.
Three ways out of the loop:
Either the condition becomes false. ==> add a println to print those values just before the end of the while loop
Or there is a break that exits ==> Add a println saying that it's breaking out of the loop
Or there is an exception that throws you out. Use try{} catch() and printStackTrace to see  Reply With Quote

5. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## Thanks, Norm. I'll report back once I've had a chance to try your suggestions.  Reply With Quote

6. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## I modified my code so that the print statements are just before the end of the while loop. But I'm still running into the same problem:

Java Code:
```				while( user_guesses < max_guesses )
{

if ((r1 == g1) & (r2 == g2) & (r3 == g3))
{
System.out.println("You won!!!");
System.out.println("You had " + incorrect + "wrong guesses.");
System.out.println("You earned: ");
System.out.println("Your balance is now x dollars.");
break;
}
else if ((r1 == g1) & (r2 == g2))
{
numCorrect = 2;
sessionSum = r1 + r2;
user_guesses++;
guessesRem--;
incorrect++;
}
else if ((r1 == g1) & (r3 == g3))
{
numCorrect = 2;
sessionSum = r1 + r3;
user_guesses++;
guessesRem--;
incorrect++;
}
else if ((r2 == g2) & (r3 == g3))
{
numCorrect = 2;
sessionSum = r2 + r3;
user_guesses++;
guessesRem--;
incorrect++;
}
else if (r1 == g1)
{
numCorrect = 1;
sessionSum = r1;
user_guesses++;
guessesRem--;
incorrect++;
}
else if (r2 == g2)
{
numCorrect = 1;
sessionSum = r2;
user_guesses++;
guessesRem--;
incorrect++;
}
else if (r3 == g3)
{
numCorrect = 1;
sessionSum = r3;
user_guesses++;
guessesRem--;
incorrect++;
}
else
{
numCorrect = 0;
sessionSum = 0;
user_guesses++;
guessesRem--;
incorrect++;

}

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
}```
Sample output:

Java Code:
```Would you like to play Mastermind (y/n)? y

Enter the amount of money you currently have: 55

Please enter difficulty level. Enter 1 for beginner, 2 for intermediate, or 3 for advanced: 3

Enter the amount you want to bet: 55

< CHEAT MODE IS ENABLED. > The answer is: 499.

Please guess the 3-digit number: 433

You have 9 guesses reamining.
Number of correct digits: 1
Sum: 4

Please guess the 3-digit number: 493

You have 8 guesses reamining.
Number of correct digits: 1
Sum: 4

Please guess the 3-digit number: 499

You have 7 guesses reamining.
Number of correct digits: 1
Sum: 4```
Any ideas as to what I'm doing wrong here?  Reply With Quote

7. ## Where is the print outs from the printlns you added to show variables values as they are changed and used?
Your "Sample output" doesn't show anything new that I can see.
Print out the values of lots of the variables so you can see what your code is doing.  Reply With Quote

8. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## Sorry, I got ahead of myself there.

Here's what I got when I tried the program out with the tracing variables displayed:

Java Code:
```Would you like to play Mastermind (y/n)? y

Enter the amount of money you currently have: 45

Please enter difficulty level. Enter 1 for beginner, 2 for intermediate, or 3 for advanced: 3

Enter the amount you want to bet: 44

< CHEAT MODE IS ENABLED. > The answer is: 143.

Please guess the 3-digit number: 142
numCorrect == 0
sessionSum == 0
user_guesses == 0
guessesRem == 10
incorrect ==0
numCorrect == 2
sessionSum == 5
user_guesses == 1
guessesRem == 9
incorrect ==1

You have 9 guesses reamining.
Number of correct digits: 2
Sum: 5
numCorrect == 2
sessionSum == 5
user_guesses == 1
guessesRem == 9
incorrect ==1

Please guess the 3-digit number: 142
numCorrect == 2
sessionSum == 5
user_guesses == 2
guessesRem == 8
incorrect ==2

You have 8 guesses reamining.
Number of correct digits: 2
Sum: 5
numCorrect == 2
sessionSum == 5
user_guesses == 2
guessesRem == 8
incorrect ==2

Please guess the 3-digit number: 143
numCorrect == 2
sessionSum == 5
user_guesses == 3
guessesRem == 7
incorrect ==3

You have 7 guesses reamining.
Number of correct digits: 2
Sum: 5
numCorrect == 2
sessionSum == 5
user_guesses == 3
guessesRem == 7
incorrect ==3  Reply With Quote

9. ## that's a start.
What about the values that control the decisions and loops?
The variables you've shown are not used in many if() tests  Reply With Quote

10. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## Followed your direction; here's what I got:
Java Code:
```Would you like to play Mastermind (y/n)? y

Enter the amount of money you currently have: 45

Please enter difficulty level. Enter 1 for beginner, 2 for intermediate, or 3 for advanced: 2

Enter the amount you want to bet: 43

< CHEAT MODE IS ENABLED. > The answer is: 696.

Please guess the 3-digit number: 604

-- DEBUGGING --
numCorrect == 0
sessionSum == 0
guessesRem == 20
incorrect ==0

- - DEBUG: CONTROLL VARS - -
user_guesses == 0
max_guesses == 20
r1 == 6
r2 == 9
r3 == 6
g1 == 6
g2 == 0
g3 == 4

-- DEBUGGING --
numCorrect == 1
sessionSum == 6
guessesRem == 19
incorrect ==1

- - DEBUG: CONTROLL VARS - -
user_guesses == 1
max_guesses == 20
r1 == 6
r2 == 9
r3 == 6
g1 == 6
g2 == 0
g3 == 4

You have 19 guesses reamining.
Number of correct digits: 1
Sum: 6

-- DEBUGGING --
numCorrect == 1
sessionSum == 6
guessesRem == 19
incorrect ==1

- - DEBUG: CONTROLL VARS - -
user_guesses == 1
max_guesses == 20
r1 == 6
r2 == 9
r3 == 6
g1 == 6
g2 == 0
g3 == 4

Please guess the 3-digit number: 606

-- DEBUGGING --
numCorrect == 1
sessionSum == 6
guessesRem == 18
incorrect ==2

- - DEBUG: CONTROLL VARS - -
user_guesses == 2
max_guesses == 20
r1 == 6
r2 == 9
r3 == 6
g1 == 6
g2 == 0
g3 == 4

You have 18 guesses reamining.
Number of correct digits: 1
Sum: 6

-- DEBUGGING --
numCorrect == 1
sessionSum == 6
guessesRem == 18
incorrect ==2

- - DEBUG: CONTROLL VARS - -
user_guesses == 2
max_guesses == 20
r1 == 6
r2 == 9
r3 == 6
g1 == 6
g2 == 0
g3 == 4

Please guess the 3-digit number: 606

-- DEBUGGING --
numCorrect == 1
sessionSum == 6
guessesRem == 17
incorrect ==3

- - DEBUG: CONTROLL VARS - -
user_guesses == 3
max_guesses == 20
r1 == 6
r2 == 9
r3 == 6
g1 == 6
g2 == 0
g3 == 4

You have 17 guesses reamining.
Number of correct digits: 1
Sum: 6

-- DEBUGGING --
numCorrect == 1
sessionSum == 6
guessesRem == 17
incorrect ==3

- - DEBUG: CONTROLL VARS - -
user_guesses == 3
max_guesses == 20
r1 == 6
r2 == 9
r3 == 6
g1 == 6
g2 == 0
g3 == 4```
I realized that the individual digits {g1, g2, g3} were not being reassigned, so I added the original calculation at the end of the while loop:

Java Code:
```				while( user_guesses < max_guesses )
{

if ((r1 == g1) & (r2 == g2) & (r3 == g3))
{
System.out.println("You won!!!");
System.out.println("You had " + incorrect + "wrong guesses.");
System.out.println("You earned: ");
System.out.println("Your balance is now x dollars.");
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);

break;
}
else if ((r1 == g1) & (r2 == g2))
{
numCorrect = 2;
sessionSum = r1 + r2;
user_guesses++;
guessesRem--;
incorrect++;
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if ((r1 == g1) & (r3 == g3))
{
numCorrect = 2;
sessionSum = r1 + r3;
user_guesses++;
guessesRem--;
incorrect++;
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if ((r2 == g2) & (r3 == g3))
{
numCorrect = 2;
sessionSum = r2 + r3;
user_guesses++;
guessesRem--;
incorrect++;
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if (r1 == g1)
{
numCorrect = 1;
sessionSum = r1;
user_guesses++;
guessesRem--;
incorrect++;
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if (r2 == g2)
{
numCorrect = 1;
sessionSum = r2;
user_guesses++;
guessesRem--;
incorrect++;
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if (r3 == g3)
{
numCorrect = 1;
sessionSum = r3;
user_guesses++;
guessesRem--;
incorrect++;
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else
{
numCorrect = 0;
sessionSum = 0;
user_guesses++;
guessesRem--;
incorrect++;
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
guessString = String.valueOf(guess);

int g1 = guess / 100;				// 	hundred's digit
int g2 = guess % 100 / 10;			// 	ten's digit
int g3 = guess % 100 % 10 / 1;		// 	one's digit

}```
I thought this would do the trick only to find a compilation error:

Java Code:
```MasterMind.java:364: g1 is already defined in main(java.lang.String[])
int g1 = guess / 100;		// 	hundred's digit
^
MasterMind.java:365: g2 is already defined in main(java.lang.String[])
int g2 = guess % 100 / 10;	// 	ten's digit
^
MasterMind.java:366: g3 is already defined in main(java.lang.String[])
int g3 = guess % 100 % 10 / 1;	// 	one's digit
^```
Do I need to rename the variables {g1, g2, g3} within this while loop?

EDIT: This might just be telling me to omit the redundant "int" declaration... I'll start there.
Last edited by serenityN0W; 07-28-2011 at 03:57 AM.  Reply With Quote

11. ## You could also move the original definitions and assignments of g1, etc down so they are inside of the while().  Reply With Quote

12. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## It works! Thanks for your help, Norm.

Java Code:
```Would you like to play Mastermind (y/n)? y

Enter the amount of money you currently have: 45

Please enter difficulty level. Enter 1 for beginner, 2 for intermediate, or 3 for advanced: 3

Enter the amount you want to bet: 43

< CHEAT MODE IS ENABLED. > The answer is: 646.

Please guess the 3-digit number: 603

-- DEBUGGING --
numCorrect == 0
sessionSum == 0
guessesRem == 10
incorrect ==0

- - DEBUG: CONTROLL VARS - -
user_guesses == 0
max_guesses == 10
r1 == 6
r2 == 4
r3 == 6
g1 == 6
g2 == 0
g3 == 3

-- DEBUGGING --
numCorrect == 1
sessionSum == 6
guessesRem == 9
incorrect ==1

- - DEBUG: CONTROLL VARS - -
user_guesses == 1
max_guesses == 10
r1 == 6
r2 == 4
r3 == 6
g1 == 6
g2 == 0
g3 == 3

You have 9 guesses reamining.
Number of correct digits: 1
Sum: 6

-- DEBUGGING --
numCorrect == 1
sessionSum == 6
guessesRem == 9
incorrect ==1

- - DEBUG: CONTROLL VARS - -
user_guesses == 1
max_guesses == 10
r1 == 6
r2 == 4
r3 == 6
g1 == 6
g2 == 0
g3 == 3

Please guess the 3-digit number: 606

-- DEBUGGING --
numCorrect == 2
sessionSum == 12
guessesRem == 8
incorrect ==2

- - DEBUG: CONTROLL VARS - -
user_guesses == 2
max_guesses == 10
r1 == 6
r2 == 4
r3 == 6
g1 == 6
g2 == 0
g3 == 6

You have 8 guesses reamining.
Number of correct digits: 2
Sum: 12

-- DEBUGGING --
numCorrect == 2
sessionSum == 12
guessesRem == 8
incorrect ==2

- - DEBUG: CONTROLL VARS - -
user_guesses == 2
max_guesses == 10
r1 == 6
r2 == 4
r3 == 6
g1 == 6
g2 == 0
g3 == 6

Please guess the 3-digit number: 646
You won!!!
You earned:
Your balance is now x dollars.

-- DEBUGGING --
numCorrect == 2
sessionSum == 12
guessesRem == 8
incorrect ==2

- - DEBUG: CONTROLL VARS - -
user_guesses == 2
max_guesses == 10
r1 == 6
r2 == 4
r3 == 6
g1 == 6
g2 == 4
g3 == 6

My program has some issues, but I cleared a major hurdle.  Reply With Quote

13. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## I moved the variable declarations within the while loop as you'd suggested. However, when I run the program and input the correct answer, this occurs:

Java Code:
```You won!!!
You earned:
Your balance is now x dollars.

You have 9 guesses reamining.
Number of correct digits: 2
Sum: 9

-- DEBUGGING --
numCorrect == 2
sessionSum == 9
guessesRem == 9
incorrect ==1

- - DEBUG: CONTROLL VARS - -
user_guesses == 1
max_guesses == 10
r1 == 7
r2 == 2
r3 == 8
g1 == 7
g2 == 2
g3 == 8

Please enter difficulty level. Enter 1 for beginner, 2 for intermediate, or 3 for advanced: ou won!!!
You eException in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at MasterMind.main(MasterMind.java:62)```

Here's the current while loop:
Java Code:
```				while( user_guesses < max_guesses )
{
int g1 = guess / 100;				// 	hundred's digit
int g2 = guess % 100 / 10;			// 	ten's digit
int g3 = guess % 100 % 10 / 1;		// 	one's digit

if ((r1 == g1) & (r2 == g2) & (r3 == g3))
{
System.out.println("You won!!!");
System.out.println("You had " + incorrect + "wrong guesses.");
System.out.println("You earned: ");
System.out.println("Your balance is now x dollars.");

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
break;
}
else if ((r1 == g1) & (r2 == g2))
{
numCorrect = 2;
sessionSum = r1 + r2;
user_guesses++;
guessesRem--;
incorrect++;

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);

System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
guessString = String.valueOf(guess);
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if ((r1 == g1) & (r3 == g3))
{
numCorrect = 2;
sessionSum = r1 + r3;
user_guesses++;
guessesRem--;
incorrect++;

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);

System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
guessString = String.valueOf(guess);
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if ((r2 == g2) & (r3 == g3))
{
numCorrect = 2;
sessionSum = r2 + r3;
user_guesses++;
guessesRem--;
incorrect++;

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);

System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
guessString = String.valueOf(guess);
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if (r1 == g1)
{
numCorrect = 1;
sessionSum = r1;
user_guesses++;
guessesRem--;
incorrect++;

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);

System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
guessString = String.valueOf(guess);
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if (r2 == g2)
{
numCorrect = 1;
sessionSum = r2;
user_guesses++;
guessesRem--;
incorrect++;

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);

System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
guessString = String.valueOf(guess);
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else if (r3 == g3)
{
numCorrect = 1;
sessionSum = r3;
user_guesses++;
guessesRem--;
incorrect++;

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);

System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
guessString = String.valueOf(guess);
// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);
}
else
{
numCorrect = 0;
sessionSum = 0;
user_guesses++;
guessesRem--;
incorrect++;

System.out.println("\nYou have " +guessesRem+" guesses reamining.");
System.out.println("Number of correct digits: " + numCorrect);
System.out.println("Sum: " + sessionSum);

System.out.print("\nPlease guess the " +secretCodeDigits+ "-digit number: ");
guess = keyboard.nextInt();
guessString = String.valueOf(guess);

// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);

}

// D E B U G
System.out.println();
System.out.println( "-- DEBUGGING --");
System.out.println( "numCorrect == " + numCorrect );
System.out.println( "sessionSum == " + sessionSum );
System.out.println( "guessesRem == " + guessesRem );
System.out.println( "incorrect ==" + incorrect );
System.out.println();
System.out.println( "- - DEBUG: CONTROLL VARS - - ");
System.out.println( "user_guesses == "+ user_guesses );
System.out.println( "max_guesses == " + max_guesses);
System.out.println( "r1 == " + r1);
System.out.println( "r2 == " + r2);
System.out.println( "r3 == " + r3);
System.out.println( "g1 == " + g1);
System.out.println( "g2 == " + g2);
System.out.println( "g3 == " + g3);

}```  Reply With Quote

14. ## You eException in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at MasterMind.main(MasterMind.java:62)
At line 62 the nextInt() call got bad data, ie it was not a number. Since user's can enter bad data, your code must check for that. The Scanner class has a method to test if the next word to be read is an int.

Its interesting that the end of the error message is mixed in with what was being printed out. Any ideas why that happened?  Reply With Quote

15. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## Thanks, I'll look through Scanner's methods.

I was wondering the same thing... no clue. I've never seen anything like that before.  Reply With Quote

16. Member Join Date
Jul 2011
Posts
10
Rep Power
0

## Should I be using public static final int WORD?  Reply With Quote

17. ## I don't see the WORD variable in your code. What do you want that for?
public final static is a way to define a GLOBAL constant that can be used by other programs.  Reply With Quote

#### Posting Permissions

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