高低猜谜游戏 - 限制用户输入的尝试次数并再次播放逻辑
Hi-Lo Guessing Game - Limiting number of attempts from user input & play again logic
我是 Java 编程新手,正在上一门大学课程,我的作业是创建一个 Hi/Lo 猜谜游戏。游戏最多为用户提供 5 次输入 1 到 100(含)之间的数字的尝试。该程序必须提供答案是否太低、太高或正确的逻辑支持。该程序必须提供在获胜或 5 次尝试失败后再次玩游戏的选项。
我已经重新创建了这个程序大约 10 次 :(。我无法让他的逻辑按照上面的说明工作。我无法在 5 次尝试时停止尝试...而且我无法让程序执行新游戏。
非常感谢任何帮助。我花了无数个小时来编写和重写这段代码,结果有很多不同——但不是预期的结果。
这是我第一次post这样做,如果post的格式不正确,我深表歉意。
我浏览了比我愿意承认的更多的论坛和示例,none 我审查并尝试实施的代码给了我将用户输入限制为每次尝试 5 次的结果,并且能够再次播放多次。
这是我的代码:
public class HiLoGuessingGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Initialize scanner and random number gennerator
Scanner input = new Scanner(System.in);
Random generator = new Random();
//State the rules of the game
System.out.println("The Hi-Lo Guessing Game. Guess a number between 1-100");
System.out.println("You have 5 attempts!");
/* define the variable Guess (user iput)
define the variable Answer (random generator)
define the variable Counter (track number of tries and limit to 5)
define the variable PlayAgain (Y/N question)*/
int guess = 0;
int answer = generator.nextInt(100)+1;
int counter = 1;
String playAgain;
boolean gameOver = false;
//Ask the Hi-Lo question - pick number 1-100 (inclusive)
//Provide feedback answer too high, too low or you win!
//Limit number of tries in the game to 5
while (guess != answer) {
System.out.print("Enter your guess: ");
guess = input.nextInt();
counter++;
if (guess < answer) {
System.out.println("Your guess " + guess + " is too low. Try again");
System.out.println("This is attempt: " + counter);
} else if (guess > answer) {
System.out.println("Your guess " + guess + " is too high. Try again");
System.out.println("This is attempt: " + counter);
} else if (guess == answer) {
System.out.println("Your guess " + guess + " is correct! You win!");
System.out.println();
System.out.println("Would you like to play again (Y/N)?");
playAgain = input.next();
}
}
if (counter ==6) {
System.out.println("Sorry, you've reached your max atttempts.");
System.out.println("Would you like to play again (Y/N)?");
playAgain = input.next();
}
// Play again logic
boolean isValid;
do {
System.out.print("Would you like to play again (Y/N)?");
playAgain = input.next().toUpperCase();
isValid = playAgain.equals("Y") || playAgain.equals("N");
playAgain = input.next();
counter = 1;
if ( !isValid ) {
System.out.println("Error, please enter Y or N");
System.out.println();
}
} while (!isValid);
}
}
您可以在 while 循环中添加一个额外的条件:
while (guess != answer && counter < 5) {
// ...
}
或者,您可以在得到正确答案时打破循环:
while (counter < 5) {
// ...
if (answer == guess){
// ...
break;
}
}
我是 Java 编程新手,正在上一门大学课程,我的作业是创建一个 Hi/Lo 猜谜游戏。游戏最多为用户提供 5 次输入 1 到 100(含)之间的数字的尝试。该程序必须提供答案是否太低、太高或正确的逻辑支持。该程序必须提供在获胜或 5 次尝试失败后再次玩游戏的选项。
我已经重新创建了这个程序大约 10 次 :(。我无法让他的逻辑按照上面的说明工作。我无法在 5 次尝试时停止尝试...而且我无法让程序执行新游戏。
非常感谢任何帮助。我花了无数个小时来编写和重写这段代码,结果有很多不同——但不是预期的结果。
这是我第一次post这样做,如果post的格式不正确,我深表歉意。
我浏览了比我愿意承认的更多的论坛和示例,none 我审查并尝试实施的代码给了我将用户输入限制为每次尝试 5 次的结果,并且能够再次播放多次。
这是我的代码:
public class HiLoGuessingGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Initialize scanner and random number gennerator
Scanner input = new Scanner(System.in);
Random generator = new Random();
//State the rules of the game
System.out.println("The Hi-Lo Guessing Game. Guess a number between 1-100");
System.out.println("You have 5 attempts!");
/* define the variable Guess (user iput)
define the variable Answer (random generator)
define the variable Counter (track number of tries and limit to 5)
define the variable PlayAgain (Y/N question)*/
int guess = 0;
int answer = generator.nextInt(100)+1;
int counter = 1;
String playAgain;
boolean gameOver = false;
//Ask the Hi-Lo question - pick number 1-100 (inclusive)
//Provide feedback answer too high, too low or you win!
//Limit number of tries in the game to 5
while (guess != answer) {
System.out.print("Enter your guess: ");
guess = input.nextInt();
counter++;
if (guess < answer) {
System.out.println("Your guess " + guess + " is too low. Try again");
System.out.println("This is attempt: " + counter);
} else if (guess > answer) {
System.out.println("Your guess " + guess + " is too high. Try again");
System.out.println("This is attempt: " + counter);
} else if (guess == answer) {
System.out.println("Your guess " + guess + " is correct! You win!");
System.out.println();
System.out.println("Would you like to play again (Y/N)?");
playAgain = input.next();
}
}
if (counter ==6) {
System.out.println("Sorry, you've reached your max atttempts.");
System.out.println("Would you like to play again (Y/N)?");
playAgain = input.next();
}
// Play again logic
boolean isValid;
do {
System.out.print("Would you like to play again (Y/N)?");
playAgain = input.next().toUpperCase();
isValid = playAgain.equals("Y") || playAgain.equals("N");
playAgain = input.next();
counter = 1;
if ( !isValid ) {
System.out.println("Error, please enter Y or N");
System.out.println();
}
} while (!isValid);
}
}
您可以在 while 循环中添加一个额外的条件:
while (guess != answer && counter < 5) {
// ...
}
或者,您可以在得到正确答案时打破循环:
while (counter < 5) {
// ...
if (answer == guess){
// ...
break;
}
}