62,614
社区成员
发帖
与我相关
我的任务
分享
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("登陆成功");
}
}
}
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!");
}