Вопрос

As part of our coursework myself and another person in my class have to make a Guessing Game where a random number is generated between 1 to 100 and the user has a maximum of 6 guesses to try and guess the number. I also have to create a "session" where the user can enter their name and it will store their results into a text file. I have got it working to a point where they can play the game and successfully input their name, but if you select the option that you want to play again, it says 'Process completed' and exits the program. Help would be greatly appreciated, here is what I have so far;

Code:

import java.util.Random; //importing the Random class
import java.util.Scanner; //importing the Scanner class

/* Author Laura Brown 28/02/2014 */

public class TheGuessingGame 
{ 
 public static void main(String[] args) 
 { 

 Random generator = new Random(); //creates a random number between 1-100
 int TARGET = generator.nextInt(100) + 1; //establishes the target number

 int guess; 
 int count = 0;
 String userName;
 String another = "y";
 Boolean flag = false;
 Boolean anotherFlag = true;

 Scanner consoleIn = new Scanner(System.in); //creating a new Scanner object
 Scanner name = new Scanner(System.in); //creating a new Scanner object

 System.out.print("Hello! Please enter your name:\n"); //asking for user input
 userName = name.nextLine();

 System.out.print("Hello "+ userName+ ", and welcome to the game!\n");

 System.out.print("Can you guess what it   is?\n");

    do { //beginning the loop
    guess = consoleIn.nextInt(); 
    count++; 

    if (guess > TARGET) 
    System.out.print("Sorry - Your guess is too high \n"); 
    else 
    if (guess < TARGET)
    System.out.print("Sorry - Your guess is too low \n"); 
    }


    while(guess != TARGET && count < 6);

    if(guess == TARGET) {
    System.out.println("Congratulations! - You found it!"); 
    System.out.println();
    }

    else {
    System.out.println("Sorry - You have used all 6 guesses");
    }

    System.out.println();
    System.out.println("Would you like to guess again? (yes/no)");
    another = consoleIn.next();

    }
}
Это было полезно?

Решение

You need to add another loop. Stylistically speaking, I would avoid using do...while loops, as they are difficult to read. I have included a while loop done the more traditional way to show you how blissfully sexy they are.

while(anotherFlag)
{
    TARGET = generator.nextInt(100) + 1; //establishes the target number

    System.out.print("Can you guess what it   is?\n");

    do 
    {   //beginning the loop
        guess = consoleIn.nextInt(); 
        count++; 

        if (guess > TARGET) 
        System.out.print("Sorry - Your guess is too high \n"); 
        else 
        if (guess < TARGET)
        System.out.print("Sorry - Your guess is too low \n"); 
    }
    while(guess != TARGET && count < 6);

    if(guess == TARGET) {
        System.out.println("Congratulations! - You found it!"); 
        System.out.println();
    }
    else 
    {
        System.out.println("Sorry - You have used all 6 guesses");
    }

    System.out.println();
    System.out.println("Would you like to guess again? (yes/no)");

    another = consoleIn.next(); 


}

The above won't work fully, you need to set anotherFlag to false if the user inputs 'no'. That should be a relatively simple exercise, but the above will get the program to loop over and over.

Другие советы

public static void main(String[] args) 
 { 

 Random generator = new Random(); //creates a random number between 1-100

 int guess; 
 int count = 0;
 int Target;
 String userName;
 String another = "y";
 Boolean flag = false;
 Boolean anotherFlag = true;

 Scanner consoleIn = new Scanner(System.in); //creating a new Scanner object
 Scanner name = new Scanner(System.in); //creating a new Scanner object

 System.out.print("Hello! Please enter your name:\n"); //asking for user input
 userName = name.nextLine();

 System.out.print("Hello "+ userName+ ", and welcome to the game!\n");

 while (another.equalsIgnoreCase("y")) // this will check if your user input "another" 
 {
 TARGET = generator.nextInt(100) + 1; //establishes the target number
 System.out.print("Can you guess what it   is?\n");


    do { //beginning the loop
    guess = consoleIn.nextInt(); 
    count++; 

    if (guess > TARGET) 
    System.out.print("Sorry - Your guess is too high \n"); 
    else 
    if (guess < TARGET)
    System.out.print("Sorry - Your guess is too low \n"); 
    }


    while(guess != TARGET && count < 6);

    if(guess == TARGET) {
    System.out.println("Congratulations! - You found it!"); 
    System.out.println();
    }

    else {
    System.out.println("Sorry - You have used all 6 guesses");
    }

    System.out.println();
    System.out.println("Would you like to guess again? (yes/no)");
    another = consoleIn.next();
    consoleIn.nextLine();

    }
 }
}

Added in another loop to loop the whole gaming process. And brought down the random generating method to be inside the loop to regenerate a new number. Try it. Anyway I added another line consoleIn.nextLine(); after your user input to clear the carriage return buffer for .next() method.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top