gpt4 book ai didi

Java 做 while 猜谜游戏

转载 作者:行者123 更新时间:2023-12-01 18:36:58 25 4
gpt4 key购买 nike

因此,对于上周我必须做的作业,我必须使用 4 个 do-while 循环和 if 语句在 Java 中制作一个猜谜游戏。我无法成功完成它,类(class)已经继续,没有为我提供任何帮助。如果有人可以查看我的代码并告诉我可以在哪里改进它以便程序正常运行,我将不胜感激。

为了简要描述该作业,该作业需要 4 个 do-while 循环:

  1. 主要的 do-while 循环包含大部分代码并保留程序一直运行直到用户想要退出
  2. 游戏 do-while 循环,使游戏保持运行直到用户猜测正确的数字,此时它将退出。 (这部分我无法弄清楚)。
  3. 游戏循环内的数字输入验证 do-while 循环,这确保用户的猜测是有效的。
  4. 非数字输入验证 do-while 循环,位于 and 之后在游戏循环之外询问用户是否想玩再次,并检查有效的“Y”或“N”响应

这是我的想法:

package week4;

//Imports
import java.lang.Math;
import java.util.Scanner;


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

//Set up scanners
Scanner again = new Scanner(System.in);
Scanner num1 = new Scanner(System.in);

//Set up variables
int userInput = 0;
int guesses = 0;
int min = 1;
int max = 100;
int range = max - min + 1;
int randNum = (int)(Math.random() * range) + min;
boolean valid = false;

//Outside loop
do{
//Describe the game
System.out.println("\nThis program is a guessing game.");
System.out.println("\nThe computer will pick a random number "
+ "between 1 and 100.");
System.out.println("\nYou will try to guess it.");
System.out.println("\nLet's play!");

//Insert Game loop here
do {
System.out.println("\nI'm thinking of a number " +
"between 1 and 100.");
//Insert valid guess checker here
do {
System.out.println("Please enter your guess: ");
if (num1.hasNextInt()){
userInput = num1.nextInt();
valid = true;
}
else {
System.out.println("Error: Please enter a whole number.\n");
num1.nextLine();
}
}while(!valid);

if (userInput > randNum) {
System.out.println("\nToo high!");
guesses++;
}
else if (userInput < randNum) {
System.out.println("\nToo Low!");
guesses++;
}
else if (userInput == randNum) {
System.out.println("You got it!");
System.out.println("It took you" + guesses + "tries");
valid = true;
}
}while(!valid);


//Insert play again checker
do {
System.out.println("\nDo you want to play again?");
System.out.println("\nEnter 'Y' if yes and 'N' if no.");
String play = again.nextLine();
if (play.equalsIgnoreCase("Y")) {
valid = true;
}
else if (play.equalsIgnoreCase("N")) {
valid = true;
}

else {
System.out.println("Error: Please answer with 'Y' or 'N'");
}
}while(!valid);

}while(!valid);
}
}

非常感谢您的帮助!

最佳答案

这是一个工作代码

Scanner scanner = new Scanner(System.in); //you only need one

int userInput = 0;
int guesses = 0;
int min = 1;
int max = 100;
int range = max - min + 1;
int randNum = (int)(Math.random() * range) + min;
boolean valid = false;
boolean playAgain = false;

do {
System.out.println("\nThis program is a guessing game.");
System.out.println("\nThe computer will pick a random number between 1 and 100.");
System.out.println("\nYou will try to guess it.");
System.out.println("\nLet's play!");

do {
System.out.println("\nI'm thinking of a number between 1 and 100.");
do {
System.out.println("Please enter your guess: ");
if (scanner.hasNextInt()){
userInput = scanner.nextInt();
valid = true;
}
else {
System.out.println("Error: Please enter a whole number.\n");
scanner.nextLine();
userInput = -1; //this is important
valid = false;
}
} while(!valid);

if (userInput == randNum) {
System.out.println("You got it!");
System.out.println("It took you " + guesses + " tries");
valid = true;
}
else {
valid = false; //this is important
++guesses;
if (userInput > randNum) {
System.out.println("\nToo high!");
}
else {
System.out.println("\nToo Low!");
}
}
} while(!valid);

do {
System.out.println("\nDo you want to play again?");
System.out.println("\nEnter 'Y' if yes and 'N' if no.");
String play = scanner.nextLine();

if (play.equalsIgnoreCase("Y")) {
valid = true;
playAgain = true;
}
else if (play.equalsIgnoreCase("N")) {
valid = true;
playAgain= false;
}
else {
valid = false;
System.out.println("Error: Please answer with 'Y' or 'N'");
}
} while(!valid);
} while(playAgain);

scanner.close();

但是,我建议使用更多函数(尤其是内部 for 循环)来保持代码易于管理且更易于理解。

关于Java 做 while 猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014639/

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