使用最小猜测的猜谜游戏在Java中不起作用
我正在使用NetBeans用Java创建一个猜谜游戏。猜测游戏允许用户猜测1到10之间的数字。每轮他们有5次机会猜出数字。这场比赛有三轮。用户完成游戏后,将输出最小猜测次数和最大猜测次数的统计数据 最小猜测不起作用,它总是输出1。现在,我已经设置了程序,以便跟踪用户每轮猜测的次数。每轮之后,它将该值与最小值和最大值进行比较。minGuess设置为5,因为猜测次数不可能超过5次。maxGuess设置为1,因为他们总是猜测一次或多次使用最小猜测的猜谜游戏在Java中不起作用,java,Java,我正在使用NetBeans用Java创建一个猜谜游戏。猜测游戏允许用户猜测1到10之间的数字。每轮他们有5次机会猜出数字。这场比赛有三轮。用户完成游戏后,将输出最小猜测次数和最大猜测次数的统计数据 最小猜测不起作用,它总是输出1。现在,我已经设置了程序,以便跟踪用户每轮猜测的次数。每轮之后,它将该值与最小值和最大值进行比较。minGuess设置为5,因为猜测次数不可能超过5次。maxGuess设置为1,因为他们总是猜测一次或多次 static void numberGuess(int guess
static void numberGuess(int guess, int randNum) { //creating a method to check if the user has guessed the correct number or if the guess should be higher or lower
if (guess < 0 | guess > 10) {
System.out.println("Please enter a valid number between 1 and 10.");
}
else if (guess == randNum) {
System.out.println("You guessed the number correctly");
}
else if (guess < randNum) {
System.out.println("Guess is too low");
}
else if (guess > randNum) {
System.out.println("Guess is too high");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/*Rational: This program allows a user to guess a number between 1 and 10 five times per round. There are three rounds in one game.
The program then outputs the stats for the game.
*/
//declaration
int userGuess; //creates a spot in memory for these variables
int numOfGuess = 0;
int invalidGuess = 0;
int minGuess = 5;
int maxGuess = 1;
int average;
Scanner Input = new Scanner (System.in); //creates an object in the scanner clas
//execution
System.out.println("Welcome to Super Guessing Game! Guess a random number between 1 and 10. There are three rounds with one guess each.");
loopOne: //labels the loop as loopTwo
for (int x = 1; x <= 3; x= x + 1 ) { //runs the loop for three rounds
System.out.println(" ");
System.out.println("Round " + x);
System.out.println("To exit the game at any point, enter a negative 1");
System.out.println(" ");
int randNum;
randNum = 1 + (int)(Math.random() * ((10 - 1) + 1)); //generates the random number
loopTwo: //labels the loop as loopTwo
for (int y = 1; y <= 5; y= y + 1) { //runs the loop five times (five guesses per round)
numOfGuess = numOfGuess + 1; //counts number of guesses user has made
System.out.println("Guess " + y + " out of 5");
System.out.println("Please guess a number between 1 and 10: ");
userGuess = Input.nextInt();
if (userGuess == -1){ //sentinel to let the user quit at any time
System.out.println("Thank you for playing");
break loopOne; //breaks out of the loops if the user wants to stop playing
}
numberGuess(userGuess, randNum); //calls the numberGuess method
if (y < minGuess) //compares to see if the minimum number of guesses is less that the number of guesses the user has made this round
minGuess = y;
if (y > maxGuess) //compares to see if the maximum number of guesses is greater than the number of guesses that the user has made this round
maxGuess = y;
if (userGuess <1 | userGuess > 10) { //keeps track of invalid guesses
invalidGuess = invalidGuess + 1;
}
if (userGuess == randNum) { //exits the round if the user guesses correctly
break;
}
}
}
average = numOfGuess / 3; //calculates the average number of guesses
System.out.println("Thanks for playing!"); //outputs the following
System.out.println("");
System.out.println("Number of Guesses Made: " + numOfGuess);
System.out.println("Average Number of Guesses: " + average);
System.out.println("Number of Invalid Guesses: " + invalidGuess);
System.out.println("Minimum Guesses Used: " + minGuess);
System.out.println("Maximum Guesses Used: " + maxGuess);
}
static void numberGuess(int guess,int randNum){//创建一个方法来检查用户是否猜到了正确的数字,或者猜测值是否应该更高或更低
如果(猜测<0 |猜测>10){
System.out.println(“请输入一个介于1和10之间的有效数字”);
}
else if(guess==randNum){
System.out.println(“你猜对了数字”);
}
else if(猜测随机数){
System.out.println(“猜测太高”);
}
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
/*Rational:这个程序允许用户每轮猜5次1到10之间的数字。一场游戏有3轮。
然后程序输出游戏的统计数据。
*/
//声明
int userGuess;//在内存中为这些变量创建一个点
int numOfGuess=0;
int invalidGuess=0;
int minGuess=5;
int maxGuess=1;
整数平均;
Scanner Input=new Scanner(System.in);//在Scanner clas中创建一个对象
//执行
System.out.println(“欢迎来到超级猜谜游戏!猜一个介于1和10之间的随机数。有三轮,每个回合猜一次。”);
loopOne://将循环标记为loopTwo
因为(intx=1;xy从一开始,并且永远不小于一,所以minGuess总是一
for (int y = 1; y <= 5; y= y + 1) {
...
if (y < minGuess)
minGuess = y;
...
}
for(int y=1;y您的if语句位于错误的位置
你每次都会问。如果用户没有猜到正确的数字
所以,只要把它放在你的numberguess方法中:
else if (guess == randNum) {
System.out.println("You guessed the number correctly");
if (y < minGuess) //compares to see if the minimum number of guesses is less that the number of guesses the user has made this round
minGuess = y;
if (y > maxGuess) //compares to see if the maximum number of guesses is greater than the number of guesses that the user has made this round
maxGuess = y;
}
else if(guess==randNum){
System.out.println(“你猜对了数字”);
if(ymaxGuess)//比较以查看最大猜测次数是否大于用户此轮的猜测次数
maxGuess=y;
}
只是想知道,不是吗((10-1)+1)
只是10
?随机数部分起作用了,只是最小的猜测次数不起作用了。是的。肯定会的。我只是想知道这样写10
有什么诀窍!:)我相信如果你自己写10,它会生成0到9之间的随机数。你必须在随机整数中加一。但是通过这样写10,它确保生成的数字在1到10之间。谢谢你的帮助。我会尝试一下。谢谢你的帮助。我会尝试一下。