java中用于验证的While循环

java中用于验证的While循环,java,Java,我想在我的“Member”构造函数中创建一个while循环(要遵循的代码-非常简单),以确保成员id是10个字符,或者pin码是4个字符 e、 g.而id/pinNumber id的长度不是10/4 停止正在创建的构造函数 打印“请输入一个id/pinNumber,长度为10/4个字符” 这是我的密码 /** * Constructor for objects of class Member */ public Member(String giveName, String giveID,

我想在我的“Member”构造函数中创建一个while循环(要遵循的代码-非常简单),以确保成员id是10个字符,或者pin码是4个字符

e、 g.而id/pinNumber id的长度不是10/4 停止正在创建的构造函数 打印“请输入一个id/pinNumber,长度为10/4个字符”

这是我的密码

 /**
 * Constructor for objects of class Member
 */
public Member(String giveName, String giveID, String givePinNumber, int giveMoney)
{
    memberName = giveName;
    id = giveID;
    if(id.length() > 10)
        System.out.println("Please input an id less than 10 characters");

    pinNumber = givePinNumber;
    if(pinNumber.length() > 10)
        System.out.println("Please input a pinNumber less than 10 characters");

    store = null;
    item = null;
    money = giveMoney;
}

我认为这种验证属于视图层: *询问价值观 *验证 *显示错误并询问正确的值 调用构造函数时,传递给它的数据必须是ok

因此,您不需要在构造函数中进行验证


实际上,构造函数的目标是初始化类的字段。没有验证,也不会抛出异常。

我认为这种验证属于视图层: *询问价值观 *验证 *显示错误并询问正确的值 调用构造函数时,传递给它的数据必须是ok

因此,您不需要在构造函数中进行验证


实际上,构造函数的目标是初始化类的字段。没有验证,也不会抛出异常。

因为这个问题似乎吸引了非常广泛/完全错误的答案,我将在这里尝试澄清一些事情:

可以在构造函数中循环以从控制台获取输入,但我强烈建议不要使用这种代码:

Member(String giveName, String giveID, String givePinNumber, int giveMoney) {
    Scanner sc = new Scanner(System.in);

    id = giveID;
    while (id.length() != 10) {
        System.out.println("Enter id with length of 10: ");
        id = sc.nextLine();
    }

    pinNumber = givePinNumber;
    while (pinNumber.length() != 4) {
        System.out.println("Enter pin-number with length of 4: ");
        pinNumber = sc.nextLine();
    }

    store = null;
    item = null;
    money = giveMoney;

    sc.close();
}

通常这将由创建成员对象的人完成!如果成员对象是在我的主方法中创建的,则可以/将执行以下操作:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String id;
    System.out.println("Please enter id: (length must be 10)");
    while ((id = sc.nextLine()).length() != 10)
        System.out.println("Length of id must be 10, try again: ");

    String pin;
    System.out.println("Please enter id: (length must be 10)");
    while ((pin = sc.nextLine()).length() != 4)
        System.out.println("Length of pin must be 4, try again: ");

    sc.close();

    Member foo = new Member("Peter", id, pin, 1000000);
}
Member(String giveName, String giveID, String givePinNumber, int giveMoney) {
    if (giveID.length() != 10)
        throw new IndexOutOfBoundsException("ID-length must be 10!");
    if (givePinNumber.length() != 4)
        throw new IndexOutOfBoundsException("PIN-length must be 4!");

    id = giveID;
    pinNumber = givePinNumber;
    store = null;
    item = null;
    money = giveMoney;

}
您的成员构造函数可以如下所示:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String id;
    System.out.println("Please enter id: (length must be 10)");
    while ((id = sc.nextLine()).length() != 10)
        System.out.println("Length of id must be 10, try again: ");

    String pin;
    System.out.println("Please enter id: (length must be 10)");
    while ((pin = sc.nextLine()).length() != 4)
        System.out.println("Length of pin must be 4, try again: ");

    sc.close();

    Member foo = new Member("Peter", id, pin, 1000000);
}
Member(String giveName, String giveID, String givePinNumber, int giveMoney) {
    if (giveID.length() != 10)
        throw new IndexOutOfBoundsException("ID-length must be 10!");
    if (givePinNumber.length() != 4)
        throw new IndexOutOfBoundsException("PIN-length must be 4!");

    id = giveID;
    pinNumber = givePinNumber;
    store = null;
    item = null;
    money = giveMoney;

}

此方法将使您的代码清晰易读,但更重要的是:可重用!考虑一个java应用程序,你没有控制台…您将无法再使用您的成员类

由于这个问题似乎吸引了非常广泛/非常错误的答案,我将尝试在这里澄清一些事情:

可以在构造函数中循环以从控制台获取输入,但我强烈建议不要使用这种代码:

Member(String giveName, String giveID, String givePinNumber, int giveMoney) {
    Scanner sc = new Scanner(System.in);

    id = giveID;
    while (id.length() != 10) {
        System.out.println("Enter id with length of 10: ");
        id = sc.nextLine();
    }

    pinNumber = givePinNumber;
    while (pinNumber.length() != 4) {
        System.out.println("Enter pin-number with length of 4: ");
        pinNumber = sc.nextLine();
    }

    store = null;
    item = null;
    money = giveMoney;

    sc.close();
}

通常这将由创建成员对象的人完成!如果成员对象是在我的主方法中创建的,则可以/将执行以下操作:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String id;
    System.out.println("Please enter id: (length must be 10)");
    while ((id = sc.nextLine()).length() != 10)
        System.out.println("Length of id must be 10, try again: ");

    String pin;
    System.out.println("Please enter id: (length must be 10)");
    while ((pin = sc.nextLine()).length() != 4)
        System.out.println("Length of pin must be 4, try again: ");

    sc.close();

    Member foo = new Member("Peter", id, pin, 1000000);
}
Member(String giveName, String giveID, String givePinNumber, int giveMoney) {
    if (giveID.length() != 10)
        throw new IndexOutOfBoundsException("ID-length must be 10!");
    if (givePinNumber.length() != 4)
        throw new IndexOutOfBoundsException("PIN-length must be 4!");

    id = giveID;
    pinNumber = givePinNumber;
    store = null;
    item = null;
    money = giveMoney;

}
您的成员构造函数可以如下所示:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String id;
    System.out.println("Please enter id: (length must be 10)");
    while ((id = sc.nextLine()).length() != 10)
        System.out.println("Length of id must be 10, try again: ");

    String pin;
    System.out.println("Please enter id: (length must be 10)");
    while ((pin = sc.nextLine()).length() != 4)
        System.out.println("Length of pin must be 4, try again: ");

    sc.close();

    Member foo = new Member("Peter", id, pin, 1000000);
}
Member(String giveName, String giveID, String givePinNumber, int giveMoney) {
    if (giveID.length() != 10)
        throw new IndexOutOfBoundsException("ID-length must be 10!");
    if (givePinNumber.length() != 4)
        throw new IndexOutOfBoundsException("PIN-length must be 4!");

    id = giveID;
    pinNumber = givePinNumber;
    store = null;
    item = null;
    money = giveMoney;

}

此方法将使您的代码清晰易读,但更重要的是:可重用!考虑一个java应用程序,你没有控制台…您将无法再使用您的成员类

在构造函数中这样做是很少见的,但这是你的决定。但是,你的问题是什么?酷,现在你的问题是什么??如果您希望
sysout
提示用户,则情况并非如此。抛出异常或将参数子串到所需的长度请记住,参数也可以是
null
,在对其调用
length()
之前,应检查这些参数。我建议改为抛出异常(非法辩论之类的)如果值与约定不匹配,则在构造函数中。然后在尝试创建实例的方法中循环。或者提供一个验证器,在其中可以检查返回适当结果的输入。但是在构造函数中循环并执行用户I/O…哎哟!在构造函数中执行类似操作是很少见的,但这是错误的您的决定。但是,您的问题是什么?很好,现在您的问题是什么?如果您希望
sysout
提示用户,则情况并非如此。抛出异常或将参数子串到所需的长度请记住,参数也可以是
null
,在调用
length()之前应检查这些参数
。我建议改为抛出一个异常(IllegalArgument或类似的东西)如果值与约定不匹配,则在构造函数中进行循环。然后在尝试创建实例的方法中进行循环。或者提供一个验证器,用于检查返回适当结果的输入。但是在构造函数中进行循环和用户I/O…哎哟!这太棒了-我的讲师告诉我在构造函数中进行验证d我不知道该怎么做,但这肯定为我澄清了,谢谢!这太棒了-我的讲师告诉我在构造函数中进行验证,我不知道该怎么做,但这肯定为我澄清了,谢谢!