模拟用户登录案例-代码实现
import java.util.Scanner;
public class StringDemo2 {
public static void main(String[] args) {
String username = "admin";
String password = "admin";
for (int x = 0; x < 3; x++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入密码:");
String pwd = sc.next();
if (name.equals(username) && pwd.equals(password)) {
System.out.println("登录成功");
break;
} else {
if ((2 - x) == 0) {
System.out.println("账号被冻结");
} else {
System.out.println("登录失败,你还有" + (2 - x) + "次机会");
}
}
}
}
}