0

成员 ID 由唯一的字母和数字序列组成,长度最多为 10,pin 号由四位数字组成,我编写了两个方法 checkId 和 checkPassword 来比较长度,然后我在构造函数中调用它们. 但代码似乎无法正常工作

public class Test
    {

    private String yourName;
    private String yourId;
    private int password;

    /**
     * Constructor for objects of class Test
     */
    public Test(String yourName, String yourId, int password)
    {
        // initialise instance variables
        this.yourName = yourName;
        this.yourId = yourId;
        this.password = password;
        checkPassword();
        checkId();
    }

    private void checkId()
    {
       int length; 
       length = yourId.length();
       if (length >= 10){
           System.out.print("the length must be less than 10, please change it");
        }
    }

    private void checkPassword()
    {
        int length;
        length = password.length();
        if (length != 4 ){
            System.out.println("must be at 4");
        }
    }
}
4

1 回答 1

4

变量password的类型为int。诸如此类的类型称为原语,因此它们没有诸如length(). 你可以这样做Integer.toString(password).length()

于 2012-11-17T19:08:38.123 回答