JAVA数字猜谜游戏使用方法:再次玩的方法不能正常工作
我是JAVA新手,已经编写了一个程序来计算人类用户选择的1到100之间的数字 程序通过猜测上下限的中点运行,用户响应为高或低,直到猜到正确的数字。但是,当节目询问您是否想再次播放时?(是/否):它没有正确输入字符。无论输入什么字符,程序都将再次运行,并且不会在输入“n”时终止 任何帮助都将不胜感激。谢谢大家!JAVA数字猜谜游戏使用方法:再次玩的方法不能正常工作,java,search,methods,binary,Java,Search,Methods,Binary,我是JAVA新手,已经编写了一个程序来计算人类用户选择的1到100之间的数字 程序通过猜测上下限的中点运行,用户响应为高或低,直到猜到正确的数字。但是,当节目询问您是否想再次播放时?(是/否):它没有正确输入字符。无论输入什么字符,程序都将再次运行,并且不会在输入“n”时终止 任何帮助都将不胜感激。谢谢大家! import java.util.Scanner; public class NumberGuesser { public static void main(String[]
import java.util.Scanner;
public class NumberGuesser
{
public static void main(String[] args)
{
do
{
playOneGame();
}
while(shouldPlayAgain());
}
//method for complete guessing number game between 1 and 100
public static void playOneGame()
{
//initial lower range to 0 and upper range to 100
int lowerBound = 0;
int upperBound = 100;
//Display message for user to guess a nummber
System.out.println("Guess a number between 1 and 100.");
//declare variables for program's number guess and user's response if number is higher or lower
int guess;
char userResponse;
do //do while loop to make number guesses until correct number is found
{
//call method to guess midpoint number
guess = Midpoint(lowerBound,upperBound);
// call method to get response from user if number is high low or correct
userResponse = getUserResponseToGuess(guess);
//decision structure if-else statements to set new lower/upper bounds after every iteration
if (userResponse == 'h' || userResponse == 'H')
{
lowerBound = guess;
}else if (userResponse == 'l' || userResponse == 'L')
{
upperBound = guess;
}else if (userResponse == 'c' || userResponse == 'C')
{
shouldPlayAgain();
playOneGame();
}
}while(userResponse != 'c' || userResponse != 'C');
}
//method to ask and allow user if they would like to play the game again
public static boolean shouldPlayAgain()
{
//Prompt User if they would like to play again
System.out.print("Great! Would you like to play again? (y/n): ");
//Read in a character
Scanner input = new Scanner(System.in);
char value = input.next().charAt(0);
//if true return y char
return value == 'y' || value == 'Y';
}
//method to get response from user for computer to guess if number is high, low, or correct
public static char getUserResponseToGuess(int guess)
{
//char response = guess;
Scanner input = new Scanner(System.in);
//declare local variable for user's response
char response;
do
{
//print out message with guess number and take input of h/l/c
System.out.print("Is the number " + guess + "? (h/l/c): ");
//Read in character
response = input.next().charAt(0);
}while(response != 'h' && response != 'l' && response != 'c'); //display guess and get user input while input does not equal h/l/c
return response;
}
//method to find the midpoint from two integers. take smaller number if 2 to return with 2 integers
public static int Midpoint(int low, int high)
{
int midpoint; //midpoint is a local variable
midpoint = (high+low)/2; //compute midpoint between upper and lower bounds
return midpoint;
}
}
在不使用返回值的情况下调用
shouldlplayreach()
if (shouldPlayAgain()){
playOneGame();
}
在不使用返回值的情况下调用
shouldlplayreach()
if (shouldPlayAgain()){
playOneGame();
}
问题在于:
}else if (userResponse == 'c' || userResponse == 'C')
{
shouldPlayAgain();
playOneGame();
}
你问球员是否想再打一轮,然后。。。不管怎么说,什么都不要做,再跑一圈。试试这个:
{
if(shouldPlayAgain()){
playOneGame();
}
}
问题在于:
}else if (userResponse == 'c' || userResponse == 'C')
{
shouldPlayAgain();
playOneGame();
}
你问球员是否想再打一轮,然后。。。不管怎么说,什么都不要做,再跑一圈。试试这个:
{
if(shouldPlayAgain()){
playOneGame();
}
}
编写此代码时,您正在迭代,代码中没有退出条件:
else if (userResponse == 'c' || userResponse == 'C')
{
shouldPlayAgain();
playOneGame();
}
你可以写:
else if(userResponse=='c'| | userResponse=='c')
{
打破
}
然后将控件返回到主循环
关于,编写此代码时,代码中没有退出条件,您正在进行迭代:
else if (userResponse == 'c' || userResponse == 'C')
{
shouldPlayAgain();
playOneGame();
}
你可以写:
else if(userResponse=='c'| | userResponse=='c')
{
打破
}
然后将控件返回到主循环
关于,请尝试运行以下代码。我已经尝试过调整您的代码,其中包括条件改进和循环删除
package test;
import java.util.Scanner;
public class NumberGuesser
{
public static void main(String[] args)
{
/*Reduntant loop removal by codechef*/
// do
// {
playOneGame();
// }
// while(/*shouldPlayAgain()*/false);
}
//method for complete guessing number game between 1 and 100
public static void playOneGame()
{
//initial lower range to 0 and upper range to 100
int lowerBound = 0;
int upperBound = 100;
//Display message for user to guess a nummber
System.out.println("Guess a number between 1 and 100.");
//declare variables for program's number guess and user's response if number is higher or lower
int guess;
char userResponse;
boolean temp = true;
one :do //do while loop to make number guesses until correct number is found
{
//call method to guess midpoint number
guess = Midpoint(lowerBound,upperBound);
// call method to get response from user if number is high low or correct
userResponse = getUserResponseToGuess(guess);
//decision structure if-else statements to set new lower/upper bounds after every iteration
if (userResponse == 'h' || userResponse == 'H')
{
lowerBound = guess;
}else if (userResponse == 'l' || userResponse == 'L')
{
upperBound = guess;
}else if (userResponse == 'c' || userResponse == 'C')
{
/* Block restructured by codechef */
temp = shouldPlayAgain();
if(temp)
{
System.out.println("TEMP : "+temp);
//playOneGame();
continue one;
}else{
System.out.println("Breaking Loop");
break one;
}
}
/*condition restructured by codechef*/
}while((userResponse != 'c' || userResponse != 'C' )&& temp);
}
//method to ask and allow user if they would like to play the game again
public static boolean shouldPlayAgain()
{
//Prompt User if they would like to play again
System.out.print("Great! Would you like to play again? (y/n): ");
//Read in a character
Scanner input = new Scanner(System.in);
char value = input.next().charAt(0);
//if true return y char
/* statement restructured by codechef */
return (value=='y'||value=='Y')?true:false;
}
//method to get response from user for computer to guess if number is high, low, or correct
public static char getUserResponseToGuess(int guess)
{
//char response = guess;
Scanner input = new Scanner(System.in);
//declare local variable for user's response
char response;
/* Reduntant While loop removal by codechef*/
// do
// {
//print out message with guess number and take input of h/l/c
System.out.print("Is the number " + guess + "? (h/l/c): ");
//Read in character
response = input.next().charAt(0);
System.out.println("RESONSE : "+response);
// }while(response != 'h' && response != 'l' && response != 'c'); //display guess and get user input while input does not equal h/l/c
return response;
}
//method to find the midpoint from two integers. take smaller number if 2 to return with 2 integers
public static int Midpoint(int low, int high)
{
int midpoint; //midpoint is a local variable
midpoint = (high+low)/2; //compute midpoint between upper and lower bounds
return midpoint;
}
}
输出:-
运行:
猜一个介于1和100之间的数字
是50号吗?(h/l/c):l
理由:l
是25号吗?(h/l/c):c
反应:c
太好了!你想再玩一次吗?(是/否):是
温度:对
是25号吗?(h/l/c):c
反应:c
太好了!你想再玩一次吗?(是/否):否
断环
如需更改,请阅读添加的注释。希望这有帮助。尝试运行以下代码。我已经尝试过调整您的代码,其中包括条件改进和循环删除
package test;
import java.util.Scanner;
public class NumberGuesser
{
public static void main(String[] args)
{
/*Reduntant loop removal by codechef*/
// do
// {
playOneGame();
// }
// while(/*shouldPlayAgain()*/false);
}
//method for complete guessing number game between 1 and 100
public static void playOneGame()
{
//initial lower range to 0 and upper range to 100
int lowerBound = 0;
int upperBound = 100;
//Display message for user to guess a nummber
System.out.println("Guess a number between 1 and 100.");
//declare variables for program's number guess and user's response if number is higher or lower
int guess;
char userResponse;
boolean temp = true;
one :do //do while loop to make number guesses until correct number is found
{
//call method to guess midpoint number
guess = Midpoint(lowerBound,upperBound);
// call method to get response from user if number is high low or correct
userResponse = getUserResponseToGuess(guess);
//decision structure if-else statements to set new lower/upper bounds after every iteration
if (userResponse == 'h' || userResponse == 'H')
{
lowerBound = guess;
}else if (userResponse == 'l' || userResponse == 'L')
{
upperBound = guess;
}else if (userResponse == 'c' || userResponse == 'C')
{
/* Block restructured by codechef */
temp = shouldPlayAgain();
if(temp)
{
System.out.println("TEMP : "+temp);
//playOneGame();
continue one;
}else{
System.out.println("Breaking Loop");
break one;
}
}
/*condition restructured by codechef*/
}while((userResponse != 'c' || userResponse != 'C' )&& temp);
}
//method to ask and allow user if they would like to play the game again
public static boolean shouldPlayAgain()
{
//Prompt User if they would like to play again
System.out.print("Great! Would you like to play again? (y/n): ");
//Read in a character
Scanner input = new Scanner(System.in);
char value = input.next().charAt(0);
//if true return y char
/* statement restructured by codechef */
return (value=='y'||value=='Y')?true:false;
}
//method to get response from user for computer to guess if number is high, low, or correct
public static char getUserResponseToGuess(int guess)
{
//char response = guess;
Scanner input = new Scanner(System.in);
//declare local variable for user's response
char response;
/* Reduntant While loop removal by codechef*/
// do
// {
//print out message with guess number and take input of h/l/c
System.out.print("Is the number " + guess + "? (h/l/c): ");
//Read in character
response = input.next().charAt(0);
System.out.println("RESONSE : "+response);
// }while(response != 'h' && response != 'l' && response != 'c'); //display guess and get user input while input does not equal h/l/c
return response;
}
//method to find the midpoint from two integers. take smaller number if 2 to return with 2 integers
public static int Midpoint(int low, int high)
{
int midpoint; //midpoint is a local variable
midpoint = (high+low)/2; //compute midpoint between upper and lower bounds
return midpoint;
}
}
输出:-
运行:
猜一个介于1和100之间的数字
是50号吗?(h/l/c):l
理由:l
是25号吗?(h/l/c):c
反应:c
太好了!你想再玩一次吗?(是/否):是
温度:对
是25号吗?(h/l/c):c
反应:c
太好了!你想再玩一次吗?(是/否):否
断环
如需更改,请阅读添加的注释。希望这能有所帮助。谢谢大家的回复!谢谢Musef,我需要更多地习惯使用中断退出条件。使用这个可以得到我想要的结果。谢谢大家的回复!谢谢Musef,我需要更多地习惯使用中断退出条件。使用这个可以得到我想要的结果。谢谢,codechef!很高兴看到如何用其他方式打破循环。感谢您改进我的冗余循环。这使代码看起来更干净。干杯@tatloh如果上述解决方案解决了您的问题,请将答案标记为正确,或至少向上投票给其他人。只需将答案标记为正确,但答案会自动取消标记。不能投票,或者至少需要15次。抱歉,新到该站点。谢谢,codechef!很高兴看到如何用其他方式打破循环。感谢您改进我的冗余循环。这使代码看起来更干净。干杯@tatloh如果上述解决方案解决了您的问题,请将答案标记为正确,或至少向上投票给其他人。只需将答案标记为正确,但答案会自动取消标记。不能投票,或者至少需要15次。抱歉,新到的网站。谢谢,Jens。很高兴知道您也可以在if语句中调用方法。尝试调用您提到的方法时,程序将重复使用“y”响应,但任何其他字符(包括“n”)都不会再次运行游戏循环,并生成最后一个猜测变量值,而不是原来的50。以下是更改后的输出:猜测1到100之间的数字。是50号吗?h是75号吗?(信用证):太好了!你想再玩一次吗?k是75号吗?o是75号吗?谢谢,詹斯。很高兴知道您也可以在if语句中调用方法。尝试调用您提到的方法时,程序将重复使用“y”响应,但任何其他字符(包括“n”)都不会再次运行游戏循环,并生成最后一个猜测变量值,而不是原来的50。以下是更改后的输出:猜测1到100之间的数字。是50号吗?h是75号吗?(信用证):太好了!你想再玩一次吗?k是75号吗?o是75号吗?(h/l/c):谢谢你的反馈,Dragondraik。这将通过调用您提到的方法产生相同的不希望的输出,程序将以“y”响应重复,但包括“n”在内的任何其他字符都不会再次运行游戏循环,并产生最后一个猜测变量值,而不是原来的50。以下是更改后的输出:猜测1到100之间的数字。是50号吗?h是75号吗?(信用证):太好了!你想再玩一次吗?k是75号吗?o是75号吗?(h/l/c):-谢谢你的反馈,Dragondraik。这将通过调用您提到的方法产生相同的不希望的输出,程序将以“y”响应重复,但包括“n”在内的任何其他字符将不会再次运行游戏循环,并产生最后一个猜测变量值,而不是原始值