Java 使用吸收和分布定律的密码游戏

Java 使用吸收和分布定律的密码游戏,java,passwords,Java,Passwords,这是我的代码。我要做的是,当我第一次为密码输入正确的值时,它只打印一次有效。但是,如果我在第一次输入正确的密码时,它会一直写“valid”。另外,如果我第一次没有输入正确的号码,那么当我输入正确的密码时,它只会停止代码。我如何解决这两个问题 public static void main(String[] args){ Scanner sc=new Scanner(System.in); int password= 9320; int user = 0;

这是我的代码。我要做的是,当我第一次为密码输入正确的值时,它只打印一次有效。但是,如果我在第一次输入正确的密码时,它会一直写“valid”。另外,如果我第一次没有输入正确的号码,那么当我输入正确的密码时,它只会停止代码。我如何解决这两个问题

    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();
    }

我主要关心的是程序一遍又一遍地说'Valid',而(user==password){循环中的任何一个变量都没有变化,因此如果它们相等,它显然会永远循环。通过附加一个调试器。“为什么这段代码不起作用?”我觉得自己像个傻瓜,但是,我该如何让循环停止?学习语言,然后不要使用循环,而是使用条件。
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");