密码游戏使用吸收和分配法则

时间:2016-12-01 16:44:42

标签: java passwords

所以,这是我的代码。我想要做的是当我第一次打印时输入正确的密码值只有一次有效。但是,如果我第一次输入正确的密码,它会不断地写入“有效”的密码。永远。此外,如果我第一次输入正确的号码,那么当我输入正确的密码时,它就会停止代码。我该如何解决这两个问题?

    public static void main(String[] args){

    Scanner sc=new Scanner(System.in);
    int password= 9320;
    int user = 0;

    System.out.print("Enter a password: :");
    user = sc.nextInt();

    while(user==password){
        System.out.println("Valid");
    }

    while(user!=password){
        System.out.println("INVALID");
        System.out.println("Enter the password again");
        user = sc.nextInt();
    }

1 个答案:

答案 0 :(得分:0)

public static void main(String[] args){

Scanner sc=new Scanner(System.in);
int password= 9320;
int user = 0;

System.out.print("Enter a password: :");
user = sc.nextInt();

while(user!=password){
    System.out.println("INVALID");
    System.out.println("Enter the password again");
    user = sc.nextInt();
}

System.out.println("Valid");