問題在代碼中已標出,請大嬸解疑,能不能這樣做,這樣做能不能實現
import javax.swing.JOptionPane;
public class PasswdException extends Exception {
PasswdException(int i) {
if (i < 6) {
System.out.println("密碼不能小於六位");
}
if (i > 6) {
System.out.println("密碼不能大於六位");
}
}
//內部類
public class PasswdExceptionisnum {
PasswdExceptionisnum(String j) {
try {
Integer.parseInt(j);
} catch (NumberFormatException e) {
System.out.println("不是數字" + e);
}
}
}
}
class Passwd {
public static boolean checkpasswd(String passwd) throws PasswdException {
if (passwd.length() == 6) {
return true;
} else
throw new PasswdException(passwd.length());
if (passwd != null) {
//問題在下一行,請問怎麼實現下一行效果
throw new PasswdException.PasswdExceptionisnum(passwd);
//上面一行行錯誤,這裡想拋出PasswdExceptionisnum內部類
}
}
public static void main(String args[]) {
try {
String passwd = JOptionPane.showInputDialog(" 請設置初始密碼");
checkpasswd(passwd);
System.out.println("密碼已經設置");
} catch (PasswdException e) {
System.out.println(e);
} finally {
System.out.println("程序結束");
}
}
}
必須使用外部類對象來創建內部類對象
PasswdException p=new PasswdException()
throw p.new PasswdExceptionisnum(passwd)