看看谁能有更简单的方法,大牛们出招吧!!!!!!
今天比较闲暇,写了一个小程序,就是一个登录相关的
从键盘中输入,用main方法接收,user:abc 和password:123
当输入错误时,应该再次输入用户名和密码,知道正确为止。
以下是我的实现方式,我想看看谁有更简单的没有,欢迎指点:
LoginEntity
public class LoginEntity { private String name; private String password; public LoginEntity(String name,String password){ this.name = name; this.password = password; } public boolean validate(){ if(name.equals("abc")&&password.equals("123")){ return true; }else{ return false; } }}import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Operate { private String str[]; public Operate(String str[]){ this.str = str; if(str.length!=2){ System.out.println("输入的参数不正确"); System.exit(1); //JVM运行终止,设置一个非0的参数即可 } } //用户再次输入 public static void inputTest(){ System.out.println("请再次输入用户名和密码"); String str[] = new String[2]; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < str.length; i++) { try { str[i] = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public boolean getInfo(){ if(new LoginEntity(this.str[0], this.str[1]).validate()){ return true; }else{ return false; } }}package mystudy.j2se.DemoLogin02;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Login { public static void main(String[] args) { System.out.println("请输入用户名和密码"); String str[] = new String[2]; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < str.length; i++) { try { str[i] = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Operate o = new Operate(str); if(!o.getInfo()){ System.out.println("登录失败"); o.inputTest(); }else{ System.out.println("登陆成功"); } } }
this.pwd = pwd;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "User [email=" + email + ", id=" + id + "]";
}
}
/**自定义异常必须继承于Exception
* 手动继承所有父类构造器
* */
class EmailExistException extends Exception{
public EmailExistException() {
super();
// TODO Auto-generated constructor stub
}
public EmailExistException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public EmailExistException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public EmailExistException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
class Pwd2ShortException extends Exception{
public Pwd2ShortException() {
super();
}
public Pwd2ShortException(String message, Throwable cause) {
super(message, cause);
}
public Pwd2ShortException(String message) {
super(message);
}
public Pwd2ShortException(Throwable cause) {
super(cause);
}
}
class EmailOrPwdException extends Exception{
public EmailOrPwdException() {
super();
// TODO Auto-generated constructor stub
}
public EmailOrPwdException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public EmailOrPwdException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public EmailOrPwdException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
[解决办法]
最简单这样:
1,验证用户名密码是否正确,如果错误,直接关闭程序,关闭前告诉他,不嫌麻烦就继续输入错误吧;
2,这样程序就很简单了,保证一次成功;
哈哈。。。。。。。。。。。。。
[解决办法]
如果你仅仅是为了求代码最简化,为啥还要搞出3个类?
public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); while (true) { System.out.print("UserName: "); String name = sc.next(); System.out.print("Password: "); String pass = sc.next(); if (name.equals("abc") && pass.equals("123"))break; System.out.println("WRONG! Please input again."); } System.out.println("Welcome baby!"); }