Discuss / Java / 交作业

交作业

Topic source
public class Demo {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.println("请输入用户名:");        String username = scanner.nextLine();        System.out.println("请输入密码:");        String password = scanner.nextLine();        String token = login(username, password);        System.out.println(token);    }    static String login(String username, String password) {        if (username.equals("admin")) {            if (password.equals("password")) {                return "登陆成功";            } else {                throw new LoginFailedException("登陆失败!\n密码错误!");            }        } else {            throw new UserNotFoundException("用户名不存在");        }    }}class UserNotFoundException extends BaseException {    public UserNotFoundException(String message) {        super(message);    }}class LoginFailedException extends BaseException {    public LoginFailedException(String message) {        super(message);    }}class BaseException extends RuntimeException {    public BaseException() {        super();    }    public BaseException(String messgae) {        super(messgae);    }    public BaseException(String message, Throwable cause) {        super(message, cause);    }    public BaseException(Throwable cause) {        super(cause);    }}

  • 1

Reply