java - 在帐户和密码java中仅显示5位数字
问题描述
我需要用户只输入 5 位数字和密码我试试这个但我仍然可以运行超过 5 位数字..
public static void login() throws IllegalLoginException {
int acc, password;
System.out.println("Welcome!");
System.out.print("Enter your account number: ");
acc = in.nextInt();
if (acc > 010000 && acc < 9999){
throw new IllegalLoginException("from Exception: Account number not enough 5 digits");
}
System.out.print("Enter your password number: ");
password = in.nextInt();
if (password >= 12345 && password <= 9999) {
throw new IllegalLoginException("from Exception: 5 Digits password required!");
}
System.out.println("\nHello there, " + acc);
}
解决方案
public static void login() throws IllegalLoginException {
int acc, password;
System.out.println("Welcome!");
System.out.print("Enter your account number: ");
acc = in.nextInt();
if (acc == null || acc.trim.length()!=5){
throw new IllegalLoginException("from Exception: Account number must be 5 digits long");
}
System.out.print("Enter your password number: ");
password = in.nextInt();
if (password == null || password.trim.length()!=5) {
throw new IllegalLoginException("from Exception: 5 Digits password required!");
}
System.out.println("\nHello there, " + acc);
}