gpt4 book ai didi

java - 最后的问题 : Mastermind Java program

转载 作者:行者123 更新时间:2023-11-30 06:58:43 24 4
gpt4 key购买 nike

我有两个关于我的代码的问题。

  1. 为什么当我输入 1 到 6 之间的数字时,输出是“哎呀,请输入 1 到 6 之间的数字”。当我尝试更具体并做出 else if 语句时,当我输入时什么也没有发生请输入一个不介于 1 到 6 之间的数字。

  2. 如何重新启动我的程序?在我的代码中,有一个 if 语句当用户输入“再次播放”时我的注释行显示Mastermind.main() 重新运行程序,但这不起作用。

代码如下:

import java.util.Scanner;

public class Mastermind {

public static void main (String [] args) {
// boolean variable to signal when the game is over.
boolean done = false;

// Scanner object
Scanner scanner = new Scanner(System.in);

// sets the value to twelve outside the loop so it doesn't set back each time.
int guesses = 12;

System.out.println("Please enter a number between 1-6 to begin (or \"quit\") to exit.");

// while loop for the game
while (!done) {

//System.out.println("Please enter a number between 1-6 (or \"quit\") to exit the game:");

// user input
String input = scanner.nextLine();
int number = 0; //Just initialized to some number


// checks to see if the user wants to quit the game.
if (input.equalsIgnoreCase("quit")) {
System.out.println("Goodbye!");
done = true;
scanner.close();
}

else{

try{
//Trying to see if the input was a number
number = Integer.parseInt(input);


}
catch(Exception e){
//The input wasn't an integer, it's invalid the starts loop again.
System.out.println("Invalid input.");
continue;
}
}

// defines necessary int variables
int random1 = (int) (Math.random() * 7);
int random2 = (int) (Math.random() * 7);
int random3 = (int) (Math.random() * 7);
int random4 = (int) (Math.random() * 7);

// If the user doesn't and decides to play, it runs this code.
// checks to see if the user enters a number between 1-6
if (number >= 1 && number <= 6) {

if (number == random1) {
System.out.println("You guessed a correct number!");
guesses--;
System.out.println("guesses = " + guesses);

}
else if (number == random2) {
System.out.println("You guessed a correct number!");
guesses--;
System.out.println("guesses = " + guesses);


}
else if (number == random3) {
System.out.println("You guessed a correct number!");
guesses--;
System.out.println("guesses = " + guesses);
}
else if (number == random4) {
System.out.println("You guessed a correct number!");
guesses--;
System.out.println("guesses = " + guesses);

}
else {

System.out.println("Sorry that's not one of the numbers! Try again.");
guesses--;
System.out.println("guesses = " + guesses);
}
}


if (guesses == 0){

System.out.println("You've run out of guesses. To play again, enter \"play again\". Otherwise, enter or \"quit\")");
if (input.equalsIgnoreCase("play again")){
// how do I restart the program?
//Mastermind.main(); // QUESTION 2
}
else if (input.equalsIgnoreCase("quit")){
System.out.println("Goodbye!");
done = true;
scanner.close();
}
else {
System.out.println("Goodbye!");
done = true;
scanner.close();
}
}
else { //QUESTION 1

System.out.println("Oops! Please choose a number between 1 and 6");
}
}
}
}

最佳答案

每当 guesses == 0 计算结果为 false 时,您都会在循环中打印该消息。您可能只需要交换两个 block 的顺序。而不是这个:

if (number >= 1 && number <= 6) {
...
}
if (guesses == 0) {
...
}
else { //QUESTION 1
System.out.println("Oops! Please choose a number between 1 and 6");
}

使用这个:

if (number >= 1 && number <= 6) {
...
}
else { //QUESTION 1
System.out.println("Oops! Please choose a number between 1 and 6");
}
if (guesses == 0) {
...
}

关于重新启动程序:如果我正确地读取了逻辑,您所需要做的就是将 done 设置为 false 并重置猜测 到 12。

另外两个逻辑点。首先,在检测到用户输入了“quit”后,您可能应该继续中断。其次,似乎您正在为每个用户猜测生成四个新的随机整数。我不知道这是否是您的意图,但您可能想稍微改变一下逻辑。这也可能会影响重启逻辑。

关于java - 最后的问题 : Mastermind Java program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41338662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com