0

メンバー ID は、長さが最大 ​​10 の文字と数字*の一意のシーケンスで構成され、ピン番号は 4 桁のシーケンスで構成されます。長さを比較するために 2 つのメソッド 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 に答える