-
September 10th, 2010, 08:31 PM
#1
help with repeating user input
I'm making a specific number guessing game called "Bagels". I have most of the work done. But once I get to the actual game, I can only input one number for it to say if it is right or not. I need the program to keep allowing numbers to be inputted until it satisfies the do/while statement.
The input statements for the guessing are in the second do/while statement towards the bottom.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class Bagels {
public static void main(String[] args) {
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Welcome to Bagels the Guessing Game!");
System.out.println("Press [Enter] to continue");
String start = new String ();
try {start = input.readLine();}
catch (IOException e) {};
String user1 = new String ();
String rulesornot = new String ();
System.out.println("To learn the rules, enter(1)");
System.out.println(" ");
System.out.println("To skip to the game, enter(2)");
try {rulesornot = input.readLine();}
catch (IOException e) {}
int rules;
rules = Integer.parseInt(rulesornot);
int userin1;
int userin2;
int userin3;
if (rules == 1) {
System.out.println("Rules: You are to guess a 3-digit number and you may not repeat a digit within an answer.");
System.out.println("If you guess correct on one number and it is in the correct spot that is \"Fermi\".");
System.out.println("If you guess correct on one number but it is not in the correct spot that is \"Pico\".");
System.out.println("If you don't guess any numbers correctly, than you will receive \"BAGELS\".");
System.out.println("Press [Enter] to continue");
String start2 = new String();// User has to press enter to continue to the game
try {start2 = input.readLine();}
catch (IOException e) {};
System.out.println("Begin guessing...");
}
else if((rules == 2) || (rules == 3))
System.out.println("Begin guessing...");
Random r = new Random();
int num;
int d1;
int d2;
int d3;
do{//generates a unique random 3 digit number
num = r.nextInt(900) + 100;
d3 = num % 10;
d2 = ((num - d3) / 10) % 10;
d1 = (num - d3 -(d2 * 10)) / 100;
}while (d1 == d2|| d1 == d3 || d2 == d3);
userin3 = num % 10;
userin2 = (num % 100) % 10;
userin1 = (num - d3 -(d2 * 10)) / 100;
int guessNumber;
do{
try {user1 = input.readLine();}//Input for guessing
catch (IOException e) {};
guessNumber = Integer.parseInt(user1);
if (d1 == userin1){
System.out.print("Fermi ");}
if (d2 == userin2){
System.out.print("Fermi ");}
if (d3 == userin3){
System.out.print("Fermi ");}
if (d1 == userin2) {
System.out.print("Pico ");}
if (d1 == userin3) {
System.out.print("Pico ");}
if (d2 == userin1){
System.out.print("Pico ");}
if (d2 == userin3){
System.out.print("Pico ");}
if (d3 == userin1){
System.out.print("Pico ");}
if (d3 == userin2){
System.out.print("Pico ");}
if ((d1 != userin1) && (d1 != userin2) && (d1 != userin3) && (d2 != userin1) && (d2 != userin2) && (d2 != userin3) && (d3 != userin1) && (d3 != userin2) && (d3 != userin3)){
System.out.println("BAGELS!");}
}while (num == guessNumber);
if (num == guessNumber){
System.out.println("You won!");}
}
}
-
September 11th, 2010, 07:52 AM
#2
Re: help with repeating user input
Your while loop continues whilst the guess is equal to the hidden number, shouldn't this be whilst it isn't equal to the hidden number. You may want to think of adding a way to allow the user to exit from the game without having to guess the correct number.
Also you input the guess number but then don't do anything with it (other than in the while statement), shouldn't you be breaking into 3 separate digits to compare with the hidden number.
-
September 11th, 2010, 05:54 PM
#3
Re: help with repeating user input
I fixed the while loop but how do I make sure the game says the right stuff to the right inputted numbers?
-
September 12th, 2010, 02:12 PM
#4
Re: help with repeating user input
how do I make sure the game says the right stuff to the right inputted numbers?
Is it printing the "wrong" stuff now?
Try debugging your program by printing out the values of all your variables so you can see how they are set and changed by the program.
For example:
System.out.println("d1=" + d1 + ", d2=" .... for the rest of the variables
Please copy the screen contents when you run the program and paste it here and add comments to it to explain what is wrong.
Norm
-
September 13th, 2010, 07:54 AM
#5
Re: help with repeating user input
but how do I make sure the game says the right stuff to the right inputted numbers?
Read the last line of my previous reply.
If you have fixed this and still have a problem post your latest code.
-
September 13th, 2010, 11:58 AM
#6
Re: help with repeating user input
I solved it on my own. Thanks. All I did wrong was mix the random digits and the user inputed digits together in my formulas.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|