【发布时间】:2022-11-22 23:51:03
【问题描述】:
需要制作一个密码程序,用户在开始时设置密码,密码可以输入3次后程序停止。该程序不能区分大小写。
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int attempts = 3;
String password = "";
System.out.println("Please input your password.");
Scanner stringScanner = new Scanner(System.in);
String PASSWORD = stringScanner.next();
while (attempts-- > 0 && !PASSWORD.equals(password)) //compares and then decrements
{
System.out.print("Enter your password: ");
password = sc.nextLine();
if (password.equals(PASSWORD))
System.out.println("Access Granted");
else
System.out.println("Incorrect. Number of attempts remaining: " + attempts);
}
if (attempts < 1) {
System.out.println("You have entered too many incorrect passwords, please try again later.");
}
else {
System.out.println("Secret: Water is not Wet.");
}
}
}
程序按预期打印,但不区分大小写
【问题讨论】:
-
在比较之前将两个字符串转换为相同的大小写。我确定这只是一个简单的练习,但请注意密码检查应该绝不在现实世界中不区分大小写。
标签: java string switch-statement