import java.util.Scanner; public class Login { private static String username = "张三"; private static String password = "123456"; public static void main(String[] args) throws ToomanyErrorsException, NamePasswordMismatchException { Scanner scanner = new Scanner(System.in); int count = 0; System.out.println("请输入用户名: "); String username = scanner.next(); while (true) { System.out.println("请输入密码: "); String password = scanner.next(); if ("登录成功".equals(login(username, password))) { System.out.println("登录成功"); break; } count++; if (count==3){ throw new ToomanyErrorsException("密码输入次数已达上限!!!"); } } } public static String login(String username, String password) throws NamePasswordMismatchException { if (!Login.username.equals(username)) { System.out.println("该用户名尚未注册,请核对后输入"); return "用户名有误"; } else { if (!Login.password.equals(password)) { try { throw new NamePasswordMismatchException("密码输入有误"); } catch (Exception e) { e.printStackTrace(); } } else { return "登录成功"; } return "密码有误"; } } }代码中缺失的补充两个自定义异常类,并成功运行程序。
时间: 2023-12-20 21:07:03 浏览: 99
java代码-实训4-3 import java.util.Scanner
以下是补充的两个自定义异常类的代码:
```
public class ToomanyErrorsException extends Exception {
public ToomanyErrorsException(String message) {
super(message);
}
}
```
```
public class NamePasswordMismatchException extends Exception {
public NamePasswordMismatchException(String message) {
super(message);
}
}
```
补充完自定义异常类后,程序可以正常运行。
阅读全文