Java simulation login game (simulate three password input scenarios)

Write code to simulate three password input scenarios. 
You can enter the password up to three times. If the password is correct, it will prompt "Login successful". If the password is incorrect, you can re-enter it up to three times. 
Three times are wrong, write code to simulate the scene of three password input. 
The password can be input up to three times. If the password is correct, it will prompt "Login successful". If the password is wrong, you can re-enter it up to three times. If it is wrong three times, it will prompt to exit the program.
mport java.util.Scanner;

public class Test14 {
    public static void main(String[] args) {
        int computerKey=123;
        int i=3;
        System.out.println("您有3次机会输入密码!");
        while(i>=0){
            System.out.println("请输入密码:");
            Scanner scanner=new Scanner(System.in);
            int key=scanner.nextInt();
            if(i==0){
                System.out.println("已锁定计算机!");
            }
            if(key==computerKey){
                System.out.println("登陆成功");
                break;
            }else{
                System.out.println("密码错误,请重新输入密码");
                System.out.println("您还有"+(i-1)+"次机会");
                System.out.println(" ");
            }

            i--;
        }

    }
}

Achieving the result:

 

Guess you like

Origin blog.csdn.net/m0_62218217/article/details/121599491