This question already has answers here:
Loop user input until conditions met
                                
                                    (3个答案)
                                
                        
                                4年前关闭。
            
                    
我目前正在通过Udemy Java课程进行学习,并且正在练习到目前为止所学的知识。

我有以下简单的程序,我正计划使用该程序来让用户输入他的名字。

import java.util.Scanner;



public class Adventure {
    public static final int menuStars = 65;
    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        String firstName = "";
        String lastName = "";
        boolean validName = false;
        while(!validName){
            //Entering first name
            System.out.println("Please enter your first name.");
            try {
                firstName = input.nextLine();
                if(firstName.length() == 0){
                    throw new Exception("Please enter a first name of at least 1 character.");
                }else{
                    //Entering last name
                    System.out.println("Please enter your last name.");
                    lastName = input.nextLine();
                    if(lastName.length() == 0){
                        throw new Exception("Please enter a last name of at least 1 character");
                    }else{
                        System.out.println("You have entered " + firstName +" " + lastName);
                    }

                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
                continue;
            }
            //Used to terminate loop when both first & last names are valid
            validName = true;
        }


    }
}


当用户输入空白名称而不是从头开始重新启动整个程序时,我想使程序重复错误消息。

例如,当用户输入一个空白的名字时,我希望程序继续重复“请输入至少一个字符的名字”,而当用户输入一个空白的姓氏时,它会继续重复“请输入一个姓氏”至少包含1个字符”,直到用户输入有效的名称为止。

但是,当前,当用户输入一个空白的名字或姓氏时,我的程序将从头开始重复自身,而不仅仅是重复错误消息。

我将如何使程序仅重复错误消息?

最佳答案

空格实际上被认为是字符,因此(length == 0)的检查不适用于您的目的。

尽管下面的代码是不完整的(例如:处理firstname =“ foo”的可能不受欢迎的情况,(请参见函数.contains()),但它会执行原始帖子所要求的操作-当用户输入空白的名字/姓氏时,它会一直重复“请输入至少一个字符的名字/姓氏”,直到用户输入有效的名字/姓氏为止。

import java.util.Scanner;

public class Adventure {
    public static final int menuStars = 65;
    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        String firstName = "";
        String lastName = "";

        boolean firstNameLegal = false;
        boolean lastNameLegal = false;

        // Entering first name
        while (!firstNameLegal) {
            System.out.println("Please enter your first name.");
            firstName = input.nextLine();

            if (!firstName.equals(" "))
                firstNameLegal = true;
            else
                System.out.println("Please enter a first name of at least 1 character.");
        }

        // Entering last name
        while(!lastNameLegal){
            System.out.println("Please enter your last name.");
            lastName = input.nextLine();

            if(!lastName.equals(" "))
                lastNameLegal = true;
            else
                System.out.println("Please enter a last name of at least 1 character.");
        }

        System.out.println("You have entered " + firstName +" " + lastName);
    }
}

07-25 21:35
查看更多