Java语言编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序
时间: 2023-05-15 12:05:50 浏览: 134
下面是 Java 代码模拟三次密码输入的场景:
```
import java.util.Scanner;
public class PasswordSimulation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String password = "123456"; // 正确的密码
int count = 0; // 输入密码的次数
while (count < 3) {
System.out.print("请输入密码:");
String input = scanner.nextLine();
if (input.equals(password)) {
System.out.println("登录成功!");
break;
} else {
count++;
if (count < 3) {
System.out.println("密码错误,请重新输入!");
} else {
System.out.println("密码错误,退出程序!");
}
}
}
}
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)