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