Angela Fife

Greenhorn
+ Follow
since Mar 18, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Angela Fife

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"));


}
}
19 years ago