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

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

invalid input from the user using loop

问题

import java.util.Scanner;
import java.util.Random;

public class ShowWhatYouKnow {

    public static void main(String[] args) {
        Scanner key = new Scanner(System.in);
        Random rd = new Random();
        String ready;
        int guess;

        int[] dice = {1, 2, 3, 4, 5, 6};
        
        System.out.println("Welcome to the dice game!");
        System.out.println();
        System.out.println("Guess a number between 1-6 and win $50!!");
        System.out.println("Are you ready?");
        ready = key.nextLine();

        // Implementing a loop to handle invalid input
        while (!ready.equalsIgnoreCase("yes") && !ready.equalsIgnoreCase("no")) {
            System.out.println("Invalid input!");
            System.out.println();
            System.out.println("Guess a number between 1-6 and win $50!!");
            System.out.println("Are you ready?");
            ready = key.nextLine();
        }

        if (ready.equalsIgnoreCase("yes")) {
            System.out.println("Let The Game Begin!!");
            System.out.println("-----------------------------------------");
            // Rest of the code for the game
        } else if(ready.equalsIgnoreCase("no")) {
            System.out.println("You Exited The Program!! Bye!");
            System.out.println("-----------------------------------------");
            System.out.println();
        }
    }
}
英文:

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:

import java.util.Scanner;
import java.util.Random;
public class ShowWhatYouKnow {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rd = new Random();
String ready;
int guess;
int[] dice = {1, 2, 3, 4, 5, 6};
System.out.println("Welcome to dice game!");
System.out.println("");
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
// trying to do a do while here so if the user inputs the invalid option this would accur again and again
/*
System.out.println("invalid input !");
System.out.println("");
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
*/
if (ready.equalsIgnoreCase("yes")) {
System.out.println("Let The Game Begin!!");
System.out.println("-----------------------------------------");
}
} else if(ready.equalsIgnoreCase("no")){
System.out.println("You Exited The Program!! Bye!");
System.out.println("-----------------------------------------");
System.out.println("");
}
}
}

答案1

得分: 0

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

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

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

    while (true) {
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
if (ready.equalsIgnoreCase("yes")) {
System.out.println("Let The Game Begin!!");
System.out.println("-----------------------------------------");
break;
} else if (ready.equalsIgnoreCase("no")) {
System.out.println("You Exited The Program!! Bye!");
System.out.println("-----------------------------------------");
System.out.println("");
break;
} else {
System.out.println("invalid input !");
}
}

答案2

得分: 0

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

public static void main(String[] args) {
    Scanner key = new Scanner(System.in);
    Random rd = new Random();
    String ready;
    int guess;

    int[] dice = {1, 2, 3, 4, 5, 6};

    System.out.println("欢迎来到骰子游戏!");
    do {
        System.out.println("");
        System.out.println("猜一个从1到6的数字,赢取 $50!!");
        System.out.println("你准备好了吗?");
        ready = key.nextLine();
    } while (!ready.equalsIgnoreCase("yes") && !ready.equalsIgnoreCase("no"));
    if (ready.equalsIgnoreCase("yes")) {
        System.out.println("让游戏开始吧!");
        System.out.println("-----------------------------------------");

    } else if (ready.equalsIgnoreCase("no")) {
        System.out.println("你退出了程序!再见!");
        System.out.println("-----------------------------------------");
        System.out.println("");
    }
}

  [1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html


<details>
<summary>英文:</summary>

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:

    public static void main(String[] args) {
        Scanner key = new Scanner(System.in);
        Random rd = new Random();
        String ready;
        int guess;

        int[] dice = {1, 2, 3, 4, 5, 6};

        System.out.println(&quot;Welcome to dice game!&quot;);
        do {
            System.out.println(&quot;&quot;);
            System.out.println(&quot;Guess a number between 1-6 and win $50!!&quot;);
            System.out.println(&quot;Are you ready??&quot;);
            ready = key.nextLine();
        } while (!ready.equalsIgnoreCase(&quot;yes&quot;) &amp;&amp; !ready.equalsIgnoreCase(&quot;no&quot;));
        if (ready.equalsIgnoreCase(&quot;yes&quot;)) {
            System.out.println(&quot;Let The Game Begin!!&quot;);
            System.out.println(&quot;-----------------------------------------&quot;);

        } else if (ready.equalsIgnoreCase(&quot;no&quot;)) {
            System.out.println(&quot;You Exited The Program!! Bye!&quot;);
            System.out.println(&quot;-----------------------------------------&quot;);
            System.out.println(&quot;&quot;);
        }
    }


  [1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

</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:

确定