无效的用户输入,使用循环。

huangapple go评论120阅读模式
英文:

invalid input from the user using loop

问题

  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class ShowWhatYouKnow {
  4. public static void main(String[] args) {
  5. Scanner key = new Scanner(System.in);
  6. Random rd = new Random();
  7. String ready;
  8. int guess;
  9. int[] dice = {1, 2, 3, 4, 5, 6};
  10. System.out.println("Welcome to the dice game!");
  11. System.out.println();
  12. System.out.println("Guess a number between 1-6 and win $50!!");
  13. System.out.println("Are you ready?");
  14. ready = key.nextLine();
  15. // Implementing a loop to handle invalid input
  16. while (!ready.equalsIgnoreCase("yes") && !ready.equalsIgnoreCase("no")) {
  17. System.out.println("Invalid input!");
  18. System.out.println();
  19. System.out.println("Guess a number between 1-6 and win $50!!");
  20. System.out.println("Are you ready?");
  21. ready = key.nextLine();
  22. }
  23. if (ready.equalsIgnoreCase("yes")) {
  24. System.out.println("Let The Game Begin!!");
  25. System.out.println("-----------------------------------------");
  26. // Rest of the code for the game
  27. } else if(ready.equalsIgnoreCase("no")) {
  28. System.out.println("You Exited The Program!! Bye!");
  29. System.out.println("-----------------------------------------");
  30. System.out.println();
  31. }
  32. }
  33. }
英文:

I am trying to make a while loop when the input from the user is invalid meaning it is not "yes" or "no" so that the question would appear again. i would appreciate it if you could help me out here!!! thanks(i have deleted most of the code except for the problem because it was too long !!)
code:

  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class ShowWhatYouKnow {
  4. public static void main(String[] args) {
  5. Scanner key = new Scanner(System.in);
  6. Random rd = new Random();
  7. String ready;
  8. int guess;
  9. int[] dice = {1, 2, 3, 4, 5, 6};
  10. System.out.println("Welcome to dice game!");
  11. System.out.println("");
  12. System.out.println("Guess a number between 1-6 and win $50!!");
  13. System.out.println("Are you ready??");
  14. ready = key.nextLine();
  15. // trying to do a do while here so if the user inputs the invalid option this would accur again and again
  16. /*
  17. System.out.println("invalid input !");
  18. System.out.println("");
  19. System.out.println("Guess a number between 1-6 and win $50!!");
  20. System.out.println("Are you ready??");
  21. ready = key.nextLine();
  22. */
  23. if (ready.equalsIgnoreCase("yes")) {
  24. System.out.println("Let The Game Begin!!");
  25. System.out.println("-----------------------------------------");
  26. }
  27. } else if(ready.equalsIgnoreCase("no")){
  28. System.out.println("You Exited The Program!! Bye!");
  29. System.out.println("-----------------------------------------");
  30. System.out.println("");
  31. }
  32. }
  33. }

答案1

得分: 0

使用一个无限循环,并在输入正确时中断它

  1. while (true) {
  2. System.out.println("猜一个1-6之间的数字,赢取$50奖金!!");
  3. System.out.println("你准备好了吗?");
  4. ready = key.nextLine();
  5. if (ready.equalsIgnoreCase("是")) {
  6. System.out.println("游戏开始!");
  7. System.out.println("-----------------------------------------");
  8. break;
  9. } else if (ready.equalsIgnoreCase("否")) {
  10. System.out.println("你退出了程序!再见!");
  11. System.out.println("-----------------------------------------");
  12. System.out.println("");
  13. break;
  14. } else {
  15. System.out.println("无效的输入!");
  16. }
  17. }
英文:

Use an infinite loop and break it when the input is correct

  1. while (true) {
  2. System.out.println("Guess a number between 1-6 and win $50!!");
  3. System.out.println("Are you ready??");
  4. ready = key.nextLine();
  5. if (ready.equalsIgnoreCase("yes")) {
  6. System.out.println("Let The Game Begin!!");
  7. System.out.println("-----------------------------------------");
  8. break;
  9. } else if (ready.equalsIgnoreCase("no")) {
  10. System.out.println("You Exited The Program!! Bye!");
  11. System.out.println("-----------------------------------------");
  12. System.out.println("");
  13. break;
  14. } else {
  15. System.out.println("invalid input !");
  16. }
  17. }

答案2

得分: 0

你可以使用 [Do-while 循环][1] 来解决你的问题:如果用户输入不是 "yes" 或 "no",问题将会再次出现。你可以像这样编写主方法:

  1. public static void main(String[] args) {
  2. Scanner key = new Scanner(System.in);
  3. Random rd = new Random();
  4. String ready;
  5. int guess;
  6. int[] dice = {1, 2, 3, 4, 5, 6};
  7. System.out.println("欢迎来到骰子游戏!");
  8. do {
  9. System.out.println("");
  10. System.out.println("猜一个从1到6的数字,赢取 $50!!");
  11. System.out.println("你准备好了吗?");
  12. ready = key.nextLine();
  13. } while (!ready.equalsIgnoreCase("yes") && !ready.equalsIgnoreCase("no"));
  14. if (ready.equalsIgnoreCase("yes")) {
  15. System.out.println("让游戏开始吧!");
  16. System.out.println("-----------------------------------------");
  17. } else if (ready.equalsIgnoreCase("no")) {
  18. System.out.println("你退出了程序!再见!");
  19. System.out.println("-----------------------------------------");
  20. System.out.println("");
  21. }
  22. }
  23. [1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
  24. <details>
  25. <summary>英文:</summary>
  26. You could use [Do-while loop][1] to solve your problem: the question will appear again if the input from user is not &quot;yes&quot; or &quot;no&quot;. You could write the main method like this:
  27. public static void main(String[] args) {
  28. Scanner key = new Scanner(System.in);
  29. Random rd = new Random();
  30. String ready;
  31. int guess;
  32. int[] dice = {1, 2, 3, 4, 5, 6};
  33. System.out.println(&quot;Welcome to dice game!&quot;);
  34. do {
  35. System.out.println(&quot;&quot;);
  36. System.out.println(&quot;Guess a number between 1-6 and win $50!!&quot;);
  37. System.out.println(&quot;Are you ready??&quot;);
  38. ready = key.nextLine();
  39. } while (!ready.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !ready.equalsIgnoreCase(&quot;no&quot;));
  40. if (ready.equalsIgnoreCase(&quot;yes&quot;)) {
  41. System.out.println(&quot;Let The Game Begin!!&quot;);
  42. System.out.println(&quot;-----------------------------------------&quot;);
  43. } else if (ready.equalsIgnoreCase(&quot;no&quot;)) {
  44. System.out.println(&quot;You Exited The Program!! Bye!&quot;);
  45. System.out.println(&quot;-----------------------------------------&quot;);
  46. System.out.println(&quot;&quot;);
  47. }
  48. }
  49. [1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
  50. </details>

huangapple
  • 本文由 发表于 2020年8月18日 10:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63460884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定