[英]How do I include a question asking the user if they want to play again?
I am still new to Java and as such I am still figuring some things out.我还是 Java 的新手,因此我仍在弄清楚一些事情。 I have been having issues with including code asking the user if they want to play again.
我在包含代码时遇到问题,询问用户是否想再次播放。 I have attempted putting it in the main class in a print statement which gave me an error.
我试图将它放在主类中的打印语句中,这给了我一个错误。 After that, I attempted putting it in the Guess.java class in multpile places but I just recieved errors.
在那之后,我尝试将它放在 Guess.java 类中的多个位置,但我只是收到错误。 I have read up on the issue and some sites have suggested a while loop but I am unsure how to implement it into my current code.
我已经阅读了这个问题,一些网站建议了一个 while 循环,但我不确定如何将它实现到我当前的代码中。 I have included both the main class which is called GuessingGame.java and the Guess.java class below.
我已经包含了名为 GuessingGame.java 的主类和下面的 Guess.java 类。 Thank you for any assistance that can be provided.
感谢您提供的任何帮助。
GuessingGame.java猜谜游戏
public class GuessingGame {
public static void main(String[] args) {
new Guess().doGuess();
}
}
Guess.java猜猜.java
class Guess {
private int answer = 0;
int tries = 0;
Scanner input = new Scanner(System.in);
int guess, i;
boolean win = false;
int amount = 10;
public Guess() {
answer = generateRandomNumber();
}
//Generate a private number between 1 and a thousand
private int generateRandomNumber() {
Random rand = new Random();
return rand.nextInt(1000) + 1;
}
public void doGuess() {
while (!win) {
System.out.println("You are limited to ten attempts."
+ " Guess a number between 1 and 1000: ");
guess = input.nextInt();
if (tries > 9) {
System.out.println("You should be able to do better!"
+ " You have hit your ten guess limit. The number"
+ " was: " + answer);
System.out.println("Do you want to play again?: ");
return;
}
if (guess > 1000) {
System.out.println("Your guess is out of the range!");
} else if (guess < 1) {
System.out.println("Your guess is out of the range!");
} else if (guess == answer) {
win = true;
tries++;
} else if (guess < answer && i != amount - 1) {
System.out.println("Your guess is too low!");
tries++;
} else if (guess > answer && i != amount - 1) {
System.out.println("Your guess is too high!");
tries++;
}
}
System.out.println("Congragulations! You guessed the number!"
+ "The number was: " + answer);
System.out.println("It took you " + tries + " tries");
}
}
You already found a good position for adding this functionality:您已经找到了添加此功能的好位置:
System.out.println("Do you want to play again?: ");
The first step now is to also tell the user what he/she should enter after that question:现在的第一步是还告诉用户他/她在该问题之后应该输入什么:
System.out.println("Do you want to play again? (enter 0 for yes and 1 for no): ");
After that we need to get the user input of course:之后,我们当然需要获取用户输入:
int number;
//If the user enters e.g. a string instead of a number, the InputMismatchException
//will be thrown and the catch-block will be executed
try {
number = input.nextInt();
//If number < 0 OR number > 1
if(number < 0 || number > 1) {
//The rest of the try-block will not be executed.
//Instead, the following catch-block will be executed.
throw new InputMismatchException();
}
break;
}
catch(InputMismatchException e) {
System.out.println("Enter 0=yes or 1=no");
//Clears the scanner to wait for the next number
//This is needed if the user enters a string instead of a number
input.nextLine();
}
If you don't know about try-catch-statements yet, I suggest to read this explanation.如果您还不了解 try-catch-statements,我建议您阅读此说明。 For details about the InputMismatchException, please see the documentation .
有关 InputMismatchException 的详细信息,请参阅文档。
The problem now is that the user only has one chance to enter 0 or 1. If the user makes a wrong input the program will just stop.现在的问题是用户只有一次机会输入 0 或 1。如果用户输入错误,程序就会停止。 One solution to this problem is to just put the code in a while-loop:
解决此问题的一种方法是将代码放入 while 循环中:
int number;
while(true) {
try {
number = input.nextInt();
if(number < 0 || number > 1) {
throw new InputMismatchException();
}
break;
}
catch(InputMismatchException e) {
System.out.println("Enter 0=yes or 1=no");
input.nextLine();
}
}
After this block, we can be sure that number
is either 0 or 1. So now we can add a simple if-statement to check the value:在这个块之后,我们可以确定
number
是 0 或 1。所以现在我们可以添加一个简单的 if 语句来检查值:
if(number == 0) {
new Guess().doGuess();
}
return;
So all in all the code looks like this:所以总而言之,代码如下所示:
System.out.println("Do you want to play again? (enter 0 for yes and 1 for no): ");
int number;
while(true) {
try {
number = input.nextInt();
if(number < 0 || number > 1) {
throw new InputMismatchException();
}
break;
}
catch(InputMismatchException e) {
System.out.println("Enter 0=yes or 1=no");
input.nextLine();
}
}
if(number == 0) {
new Guess().doGuess();
}
return;
Don't forget to add the following import-statements:不要忘记添加以下导入语句:
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
Try this.尝试这个。 Basically, if the user responds with "yes" , we will call the function again.
基本上,如果用户响应“是”,我们将再次调用该函数。
if (tries > 9) {
System.out.println("You should be able to do better!"
+ " You have hit your ten guess limit. The number" + " was: " + answer);
System.out.println("Do you want to play again? (yes/no): "); // modified line
if("yes".equalsIgnoreCase(input.next())){ // newly added if block
answer = generateRandomNumber();
tries=0;
i=0;
win = false;
doGuess();
}
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.