import java.util.Scanner;
/*
*登录功能,用户名和密码
*三次机会
*判断数组是否相同
*String(s.equal(""))
*for循环控制次数
*/
public class StringTest {
public static void main(String[] args) {
String username = "admin";
String password = "admin";
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名");
Scanner s = new Scanner(System.in);
String uname = s.nextLine();
System.out.println("请输入密码");
s = new Scanner(System.in);
String pwd = s.nextLine();
if (uname.equals(username) && pwd.equals(password)) {
System.out.println("登录成功");
break;
} else if ((2 - i == 0)) {
System.out.println("错误次数达到上限,账户冻结");
break;
} else {
System.out.println("密码错误,你还有" + (2 - i) + "次机会");
}
}
}
}
|
|