我的问题是如何将用户输入限制为Y / N或y / n(在Java中)。目前我正在使用equals()并计划将它们更改为equalsignorecase(),这应该处理案例部分。但是,这并不能阻止用户输入其他字符(例如:H或h)。目前,当输入除y或n之外的字符时,程序直接进入"感谢播放消息"和游戏的结束。
我对编程比较陌生,所以请提供一些建议,最好是一个完整的例子。这对我来说真的很长。另外,如果你觉得这部分代码可以用更好的方式编写,我可以重写,但请再次提供一个完整的例子。
我意识到这个问题对于stackoverflow来说有点宽泛,但我真的可以使用更有经验的程序的洞察力。谢谢你的时间。
// creates instance of BufferedReader
// prompts user to play the game again
// places user input in a try
// if user wants to play again, call startGame()
// if user dosen't want to play again, keep asking anyways
private void showPlayAgainMessage()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println();
System.out.println("Do you want to play again? (y/n)");
try
{
String playAgain = br.readLine();
// Do you want to play again? Is y.
if(playAgain.equals("y"))
{
startGame();//else prompt another question with if else
}
// Do you want to play again? Is n.
else if(playAgain.equals("n"))
{
System.out.println();
System.out.println("Last chance. Play again? (y/n)");
playAgain = br.readLine();
// Last chance. Play again? Is y.
if(playAgain.equals("y"))
{
startGame();
}
// Last chance. Play again? Is n.
else if(playAgain.equals("n"))
{
System.out.println();
System.out.println("How about Minesweeper? (y/n)");
playAgain = br.readLine();
// How about Minesweeper? Is y.
if(playAgain.equals("y"))
{
System.out.println();
System.out.println("I really wish we had Minesweeper...");
System.out.println("Lots of Hangman though...Hangman? (y/n)");
playAgain = br.readLine();
// Lots of Hangman though...Hangman? Is y.
if(playAgain.equals("y"))
{
startGame();
}
// Lots of Hangman though...Hangman? Is n.
else if (playAgain.equals("n"))
{
System.out.println();
System.out.println("ok...");
}
}
}
}
}
答案 0 :(得分:0)
这个问题看起来很烦人,但是如果必须的话,可以使用while循环和一个实现状态机的switch语句。
int state = 0;
while (state < 4) {
switch (state) {
case 0: System.out.println("Do you want to play again? (y/n)"); break;
case 1: System.out.println("Last chance. Play again? (y/n)"); break;
case 2: System.out.println("How about Minesweeper? (y/n)"); break;
case 3: System.out.println("I really wish we had Minesweeper...");
System.out.println("Lots of Hangman though...Hangman? (y/n)");break;
}
String playAgain = br.readLine();
if(playAgain.equals("y"))
{
startGame();
state = 0;
}
else if(playAgain.equals("n")) {
state++;
}
}
System.out.println("ok...");
每个&#34; n&#34;回答会让你进入下一个问题。非法输入将再次显示当前问题。
答案 1 :(得分:0)
创建一个检查用户输入并在任何地方调用它的函数。
public boolean checkInput(String in){
if(in.equalsIgnoreCase("y") || in.equalsIgnoreCase("n"))
return true;
else
return false;
}
从您的代码库中调用此方法,例如
System.out.println();
System.out.println("Do you want to play again? (y/n)");
try
{
String playAgain = br.readLine();
while(!checkInput){
System.out.println("Please enter Valid Input Y/N");
playAgain = br.readLine();
}
// Do you want to play again? Is y.
if(playAgain.equals("y"))
{
startGame();//else prompt another question with if else
}
此处while循环中的代码不允许用户输入任何无效输入,如h,g等。它将提示用户输入无效,并且应输入有效选项。
答案 2 :(得分:0)
你需要简单地这样做,即为y / n以外的字符添加其他部分。只需在else部分调用showPlayAgainMessage()函数。
您可以为代码的其他必需部分执行此操作。
private void showPlayAgainMessage()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println();
System.out.println("Do you want to play again? (y/n)");
try
{
if (playAgain.equals("y")) {
startGame();
}
else if (playAgain.equals("n")) {
System.out.println();
System.out.println("Last chance. Play again? (y/n)");
playAgain = br.readLine();
if (playAgain.equals("y")) {
System.out.println();
System.out.println("I really wish we had Minesweeper...");
System.out.println("Lots of Hangman though...Hangman? (y/n)");
playAgain = br.readLine();
// Lots of Hangman though...Hangman? Is y.
if (playAgain.equals("y")) {
startGame();
} // Lots of Hangman though...Hangman? Is n.
else if (playAgain.equals("n")) {
System.out.println();
System.out.println("ok...");
}
}
}
else{
System.out.println("Sorry, invalid input. y/n required.");
showPlayAgainMessage(); // this will prompt the user again for y/n if any other character is typed.
}
}catch(Exception e){
}
}