# problem with while loop

Angela Fife

Greenhorn

Posts: 1

posted 10 years ago

I am having issues with a program I wrote for a school project. The program works just fine the first time the user plays the game; but, after the user is prompted to play again, the program skips the part asking for input from the user about the digits he wants to hint at and goes straight into asking the user to input the sum of the remaining digits. Why?

If you have any advice on how to fix this it would be greatly apreciated.

/*

*GuessingGame.java

*

*Angela Fife

*

*The purpose of this program is to simulate a magical guessing game

*

*/

import java.util.Scanner;

import java.util.Random;

public class GuessingGame{

public static void main(String [] args){

Scanner scan = new Scanner(System.in);

Random gen = new Random();

int quotient;

int num = 0;//computer's randomly generated number based on the level of difficulty

int difficulty = 0;//player's choice of difficulty level of game

int sumNum = 0;//the sum of the digits of the randomly picked number

int randomDigit;//to help in the calculation of the sum of the digits of the number

int evenOrOdd;//helps in the determination of even or odd for calculation purposes

int transformedNum;//the random number transfored into a multiple of 9

int count = 0;

int digit=0;//players choice of a digit in the sum of the scrambled numbers

int sumHint = 0;//sum of the numbers the user hinted at during the game.

int remainingSum;//sum of the remaining numbers after the user identifies other numbers

int finalNum;

String playAgain = "y";

System.out.println("Welcome to my magical number guessing game!");

System.out.println();

do{

System.out.println();

System.out.println("Please choose the difficulty level of the game.");

System.out.println("1-Easy (3 digits), 2-Average (5 digits), or 3-Hard"+

" (8 digits).");

System.out.print("Enter choice (1/2/3): ");

difficulty = scan.nextInt();

if (difficulty == 1){

num = gen.nextInt(899)+101;//random number with 3 digits

}

else

if (difficulty == 2){

//this generates a random number with 5 digits

num = gen.nextInt(89999)+10001;

}

else

if(difficulty == 3){

//this generates a random number with 8 digits

num = gen.nextInt(89999999)+10000001;

}

//this step finds the sum of the number

quotient = num;

while (quotient>0){

randomDigit=quotient%10;

sumNum += randomDigit;

quotient = quotient/10;

}

//this step determines if the number is even or odd

evenOrOdd = num%2;

if (evenOrOdd == 0){

transformedNum = num + 18 - sumNum;

}

else

transformedNum = num + 8 * sumNum;

//the next group is a list of instructions for the user

System.out.println("The trick's number is: " + transformedNum);

System.out.println();

System.out.println("Please, scramble the number's digits to form as"+

" many numbers as you want.");

System.out.println("All the new numbers must contain the same digits"+

" as "+ transformedNum+".");

System.out.println("Now, add all these numbers up and circle a non-zero"+

" digit in the result." );

System.out.println();

//this step asks for the user to input some of the digits from the number that they calculated

if (transformedNum >= 100 && transformedNum < 10000){//for a 3 digit number

while (count<2 && digit != (-1)){

System.out.println("Please, enter one of the remaining digits"+

" in any order you want");

System.out.print("You may enter a -1 to quit: ");

digit = scan.nextInt();

if (digit == -1){

sumHint += digit +1;//adds the hint digits together

count ++;

}

else

sumHint += digit;

count++;

}

}

else

if (transformedNum >= 10000 && transformedNum < 1000000){//for a 5 digit number

while (count<4 && digit != (-1)){

System.out.println("Please, enter one of the remaining"+

" digits in any order you want.");

System.out.print("You may enter a -1 to quit: ");

digit = scan.nextInt();

if (digit == -1){

sumHint += digit +1;//adds the hint digits together

count ++;

}

else

sumHint += digit;

count++;

}

}

else

//for an 8 digit number

while (count<7 && digit != (-1)){

System.out.println("Please, enter one of the remaining digits"+

" in any order you want.");

System.out.print("You may enter a -1 to quit: ");

digit = scan.nextInt();

if (digit == -1){

sumHint += digit +1;//adds the hint digits together

count ++;

}

else

sumHint += digit;

count++;

}

//this step asks for the sum of the remaining digits

System.out.println();

System.out.print("Please enter the sum of the remaining digits (not "+

"including your number and the one's you have already told me.) ");

remainingSum = scan.nextInt();

System.out.println();

//this step calculates the magic number

finalNum = (sumHint + remainingSum)%9;

if (finalNum == 0){

System.out.println("Your number is 9!");

}

else

System.out.println("Your number is " + (9-finalNum) + "!");

System.out.println();

System.out.print("Would you like to play again? (y/n) ");

playAgain = scan.next();

}while (playAgain.equalsIgnoreCase("Y"));

}

}

If you have any advice on how to fix this it would be greatly apreciated.

/*

*GuessingGame.java

*

*Angela Fife

*

*The purpose of this program is to simulate a magical guessing game

*

*/

import java.util.Scanner;

import java.util.Random;

public class GuessingGame{

public static void main(String [] args){

Scanner scan = new Scanner(System.in);

Random gen = new Random();

int quotient;

int num = 0;//computer's randomly generated number based on the level of difficulty

int difficulty = 0;//player's choice of difficulty level of game

int sumNum = 0;//the sum of the digits of the randomly picked number

int randomDigit;//to help in the calculation of the sum of the digits of the number

int evenOrOdd;//helps in the determination of even or odd for calculation purposes

int transformedNum;//the random number transfored into a multiple of 9

int count = 0;

int digit=0;//players choice of a digit in the sum of the scrambled numbers

int sumHint = 0;//sum of the numbers the user hinted at during the game.

int remainingSum;//sum of the remaining numbers after the user identifies other numbers

int finalNum;

String playAgain = "y";

System.out.println("Welcome to my magical number guessing game!");

System.out.println();

do{

System.out.println();

System.out.println("Please choose the difficulty level of the game.");

System.out.println("1-Easy (3 digits), 2-Average (5 digits), or 3-Hard"+

" (8 digits).");

System.out.print("Enter choice (1/2/3): ");

difficulty = scan.nextInt();

if (difficulty == 1){

num = gen.nextInt(899)+101;//random number with 3 digits

}

else

if (difficulty == 2){

//this generates a random number with 5 digits

num = gen.nextInt(89999)+10001;

}

else

if(difficulty == 3){

//this generates a random number with 8 digits

num = gen.nextInt(89999999)+10000001;

}

//this step finds the sum of the number

quotient = num;

while (quotient>0){

randomDigit=quotient%10;

sumNum += randomDigit;

quotient = quotient/10;

}

//this step determines if the number is even or odd

evenOrOdd = num%2;

if (evenOrOdd == 0){

transformedNum = num + 18 - sumNum;

}

else

transformedNum = num + 8 * sumNum;

//the next group is a list of instructions for the user

System.out.println("The trick's number is: " + transformedNum);

System.out.println();

System.out.println("Please, scramble the number's digits to form as"+

" many numbers as you want.");

System.out.println("All the new numbers must contain the same digits"+

" as "+ transformedNum+".");

System.out.println("Now, add all these numbers up and circle a non-zero"+

" digit in the result." );

System.out.println();

//this step asks for the user to input some of the digits from the number that they calculated

if (transformedNum >= 100 && transformedNum < 10000){//for a 3 digit number

while (count<2 && digit != (-1)){

System.out.println("Please, enter one of the remaining digits"+

" in any order you want");

System.out.print("You may enter a -1 to quit: ");

digit = scan.nextInt();

if (digit == -1){

sumHint += digit +1;//adds the hint digits together

count ++;

}

else

sumHint += digit;

count++;

}

}

else

if (transformedNum >= 10000 && transformedNum < 1000000){//for a 5 digit number

while (count<4 && digit != (-1)){

System.out.println("Please, enter one of the remaining"+

" digits in any order you want.");

System.out.print("You may enter a -1 to quit: ");

digit = scan.nextInt();

if (digit == -1){

sumHint += digit +1;//adds the hint digits together

count ++;

}

else

sumHint += digit;

count++;

}

}

else

//for an 8 digit number

while (count<7 && digit != (-1)){

System.out.println("Please, enter one of the remaining digits"+

" in any order you want.");

System.out.print("You may enter a -1 to quit: ");

digit = scan.nextInt();

if (digit == -1){

sumHint += digit +1;//adds the hint digits together

count ++;

}

else

sumHint += digit;

count++;

}

//this step asks for the sum of the remaining digits

System.out.println();

System.out.print("Please enter the sum of the remaining digits (not "+

"including your number and the one's you have already told me.) ");

remainingSum = scan.nextInt();

System.out.println();

//this step calculates the magic number

finalNum = (sumHint + remainingSum)%9;

if (finalNum == 0){

System.out.println("Your number is 9!");

}

else

System.out.println("Your number is " + (9-finalNum) + "!");

System.out.println();

System.out.print("Would you like to play again? (y/n) ");

playAgain = scan.next();

}while (playAgain.equalsIgnoreCase("Y"));

}

}

posted 10 years ago

Hi,

Welcome to JavaRanch!

The program assumes that "count" is 0 when the user hasn't input any digits, but in fact this is only true the first time through the loop. On subsequent trips, the previous non-zero value of "count" is still there. You need to set count to 0

There may be more variables like this, but that's the first one I found. I think you probably want to move the declarations of most (perhaps not all, but most) of the variables declared at the top of main inside the do-while loop, so they're fresh each time through.

Welcome to JavaRanch!

The program assumes that "count" is 0 when the user hasn't input any digits, but in fact this is only true the first time through the loop. On subsequent trips, the previous non-zero value of "count" is still there. You need to set count to 0

*inside*the big do-while loop rather than outside.There may be more variables like this, but that's the first one I found. I think you probably want to move the declarations of most (perhaps not all, but most) of the variables declared at the top of main inside the do-while loop, so they're fresh each time through.