• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

problem with while loop

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"));


}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic