【问题标题】:invalid input from the user using loop用户使用循环的无效输入
【发布时间】:2020-08-18 02:05:28
【问题描述】:

当用户的输入无效时,我正在尝试进行 while 循环,这意味着它不是“是”或“否”,这样问题就会再次出现。如果你能在这里帮助我,我将不胜感激!!!谢谢(我已经删除了大部分代码,除了问题,因为它太长了!!) 代码:

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("");
        }
        

    

}
}

【问题讨论】:

  • 也许看看添加另一个?这看起来有点像 UNI/学校的作业。

标签: javascript java arrays loops while-loop


【解决方案1】:

您可以使用Do-while loop 来解决您的问题:如果用户的输入不是“是”或“否”,问题将再次出现。你可以这样写 main 方法:

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!");
    do {
        System.out.println("");
        System.out.println("Guess a number between 1-6 and win $50!!");
        System.out.println("Are you ready??");
        ready = key.nextLine();
    } while (!ready.equalsIgnoreCase("yes") && !ready.equalsIgnoreCase("no"));
    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("");
    }
}

【讨论】:

    【解决方案2】:

    使用无限循环并在输入正确时将其中断

        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 !");
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多