用户输入以Java重复程序
我正在编写一个简单的猜谜游戏程序,用户将输入一个数字来尝试猜测随机生成的数字。
如果他们得到了正确的数字我想给他们再次玩的选项。
这是我的代码:
public class GuessingGame { private Random num = new Random(); private int answer = num.nextInt(10); private int guess; private String playAgain; public void inputGuess(){ System.out.println("Enter a number between 1 and 10 as your first guess: "); Scanner input = new Scanner(System.in); guess = input.nextInt(); do{ if (guess 10){ System.out.println("That is not a valid entry. Please try again: "); guess = input.nextInt(); }else if (guess > answer){ System.out.println("Too high, Try Again: "); guess = input.nextInt(); }else if (guess < answer){ System.out.println("Too low, Try Again: "); guess = input.nextInt(); } }while (guess != answer); System.out.println("Congratulations, You guessed the number!"); System.out.println("Would you like to play again? Enter Y to play or any other key to quit: "); playAgain = input.nextLine(); if(playAgain == "Y" || playAgain == "y"){ System.out.println("Enter a number between 1 and 10 as your first guess: "); guess = input.nextInt(); } } }
游戏播放但是当用户被提示再次播放时没有任何反应?
有什么建议么?
这是完整的代码,完全工作和测试…不使用递归..一切都修复。
public static void main(String[] args) { String playAgain = ""; Scanner scan = new Scanner(System.in); do { ClassName.inputGuess(); System.out.println("Would you like to play again? Enter Y to play or any other key to quit: "); playAgain = scan.nextLine(); } while(playAgain.equalsIgnoreCase("Y")); System.out.println("Thanks for playing!"); } public void inputGuess() { Random num = new Random(); int answer = num.nextInt(10)+1; Scanner input = new Scanner(System.in); int guess; System.out.println("Enter a number between 1 and 10 as your first guess: "); guess = input.nextInt(); do { if (guess < 1 || guess > 10) { System.out.println("That is not a valid entry. Please try again: "); guess = input.nextInt(); } else if (guess > answer) { System.out.println("Too high, Try Again: "); guess = input.nextInt(); } else if (guess < answer) { System.out.println("Too low, Try Again: "); guess = input.nextInt(); } input.nextLine(); } while (guess != answer); System.out.println("Congratulations, You guessed the number!"); }
执行以下操作,您的代码将起作用:
- 替换所有
input.nextInt();
withInteger.parseInt(input.nextLine());
- 用
(playAgain.equalsIgnoreCase("Y"))
替换(playAgain == "Y" || playAgain == "y")
(playAgain.equalsIgnoreCase("Y"))
- 在
inputGuess()
内部初始化answer
。 - 用
inputGuess();
替换if(playAgain.equalIgnoreCase("Y"))
的inputGuess();
当您通过控制台输入整数值时,它还包含\n
(下一行)。 但是当你使用nextInt()
,它不会读取这个\n
,但是当你尝试使用input.nextLine()
获取下一行时,它会查找已经存在于整数条目中的\n
(下一行)之后什么都没有。 代码查找“Y”或“y”并中断,因为它没有找到任何一个。
这就是Integer.parseInt(input.nextLine());
在这里工作
这是代码:
private Random num = new Random(); private int answer = num.nextInt(10) +1; private int guess; private String playAgain; Scanner input = new Scanner(System.in); public void inputGuess(){ System.out.println("Enter a number between 1 and 10 as your first guess: "); guess = input.nextInt(); do{ if (guess < 1 || guess > 10){ System.out.println("That is not a valid entry. Please try again: "); guess = input.nextInt(); }else if (guess > answer){ System.out.println("Too high, Try Again: "); guess = input.nextInt(); }else if (guess < answer){ System.out.println("Too low, Try Again: "); guess = input.nextInt(); } if(guess == answer) { System.out.println("Congratulations, You guessed the number!"); System.out.println("Would you like to play again? Enter Y to play or any other key to quit: "); playAgain = input.nextLine(); } }while (!playAgain.equals("Y") && !playAgain.equals("y")); }
你只需要在while内引入输赢逻辑,条件就是结束/继续标志。
在比较字符串以使用equals方法时,另一件事总是要记住,因为==将比较对象引用而不是String值,在某些情况下==将返回true以获得相等的字符串,因为JVM如何存储字符串,但是确保总是使用等于 。
尝试这样的事情:
public void inputGuess(){ System.out.println("Enter a number between 1 and 10 as your first guess: "); Scanner input = new Scanner(System.in); guess = input.nextInt(); playAgain = "Y"; do{ if (guess < 1 || guess > 10){ System.out.println("That is not a valid entry. Please try again: "); guess = input.nextInt(); }else if (guess > answer){ System.out.println("Too high, Try Again: "); guess = input.nextInt(); }else if (guess < answer){ System.out.println("Too low, Try Again: "); guess = input.nextInt(); } if(guess == answer) { System.out.println("Congratulations, You guessed the number!"); System.out.println("Would you like to play again? Enter Y to play or N to quit: "); input.nextLine(); playAgain = input.next(); answer = num.nextInt(10); guess = -1; if(!playAgain.equalsIgnoreCase("N")) { System.out.println("Enter a number between 1 and 10 as your first guess: "); guess = input.nextInt(); } } }while (!playAgain.equalsIgnoreCase("N")); }
您需要使用代码来检查是否要在循环内再次播放。 这样你就等到他们正确猜到了这个号码,然后问他们是否想再玩一次。 如果他们这样做,如果他们不退出循环,则重新启动该过程。
我上面看到的一些解决方案是不正确的。 随机数,你需要加1来得到1到10之间,你还需要与equals进行比较。 我在这里使用不区分大小写。
以下代码可以根据需要使用。
import java.util.Random; import java.util.Scanner; public class Test2 { private static Random num = new Random(); private static int answer = 0; private static int guess; private static String playAgain; public static void main(String[] args) { inputGuess(); } // Guess Method. public static void inputGuess(){ // create answer. answer = 1+ num.nextInt(10); System.out.println("Enter a number between 1 and 10 as your first guess: "); Scanner input = new Scanner(System.in); guess = input.nextInt(); do{ if (guess < 1 || guess > 10){ System.out.println("That is not a valid entry. Please try again: "); guess = input.nextInt(); }else if (guess > answer){ System.out.println("Too high, Try Again: "); guess = input.nextInt(); }else if (guess < answer){ System.out.println("Too low, Try Again: "); guess = input.nextInt(); } }while (guess != answer); System.out.println("Congratulations, You guessed the number!"); System.out.println("Would you like to play again? Enter Y to play or any other key to quit: "); playAgain = input.nextLine(); if( playAgain.equalsIgnoreCase("Y") ){ inputGuess(); } } }
到现在为止,你已经猜到了正确的方法。 以下是我将如何处理它
public class Test { public static void main (String...a) { inputGuess(); } public static void inputGuess() { Scanner input = new Scanner(System.in); String playAgain = "Y"; int guess; Random ran = new Random(); int answer = ran.nextInt(10) + 1; while (playAgain.equalsIgnoreCase("Y")) { System.out.println("Enter a number between 1 and 10 as your first guess: " + answer); guess = input.nextInt(); do { if (guess < 1 || guess > 10) { System.out.println("That is not a valid entry. Please try again: "); guess = input.nextInt(); } else if (guess > answer) { System.out.println("Too high, Try Again: "); guess = input.nextInt(); } else if (guess < answer) { System.out.println("Too low, Try Again: "); guess = input.nextInt(); } } while (guess != answer); System.out.println("Congratulations, You guessed the number!"); System.out.println("Would you like to play again? Enter Y to play or any other key to quit: "); input.nextLine(); playAgain = input.nextLine(); answer = ran.nextInt(10) + 1 } } }
此代码可以满足您的目的……
import java.util.Random; import java.util.Scanner; public class GuessingGame { private Random num = new Random(); private int answer ; private int guess; private String playAgain; public void inputGuess() { answer = num.nextInt(11); System.out.println("Enter a number between 1 and 10 as your first guess: "); Scanner input = new Scanner(System.in); guess = input.nextInt(); do { if (guess < 1 || guess > 10) { System.out .println("That is not a valid entry. Please try again: "); guess = input.nextInt(); } else if (guess > answer) { System.out.println("Too high, Try Again: "); guess = input.nextInt(); } else if (guess < answer) { System.out.println("Too low, Try Again: "); guess = input.nextInt(); } } while (guess != answer); System.out.println("Congratulations, You guessed the number!"); System.out.println("Would you like to play again? Enter Y to play or any other key to quit: "); do { playAgain = input.nextLine(); }while(playAgain.length()<1); if (playAgain.trim().equalsIgnoreCase("y")) { inputGuess(); } else { System.out.println("Good Bye!!!"); } } public static void main(String[] args) { new GuessingGame().inputGuess(); } }
一个问题 :
不要用==
来比较两个字符串的内容,你应该使用equals()
想象一下,在这种情况下,正确答案就是一个
请将此作为蓝图样本
int answer = 0; String yes = ""; Scanner input = new Scanner(System.in); do{ do{ System.out.println("Enter your number"); answer = input.nextInt(); } while ( answer != 1); System.out.println("Would you like to play again?"); yes = input.next(); } while ( yes.equalsIgnoreCase("yes"));
输出: