用户使用循环输入无效的信息

问题描述

用户输入无效时,我试图做一个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("");
        }
        

    

}
}

解决方法

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

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

您可以使用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("");
    }
}