How to solve the else/if problems in password input?

狂风中的少年 提交于 2019-12-26 21:39:26

问题


I wrote a program that users can login. Below u see some codes that I wrote for the password input but the second if of them does not work properly.

Please help me to find the problem. Why it does not work?

import java.util.Scanner;

public class Password {

    public static void main(String[] args) {
        Scanner passwordInput = new Scanner(System.in);
        System.out.print("Please enter your password: ");
        int builtInPassword = 1254;

        if (passwordInput.hasNextInt() && passwordInput.nextInt() == builtInPassword) {
            System.out.println("Your password is correct.");
        } else if (passwordInput.hasNextInt() && passwordInput.nextInt() != builtInPassword) {
            System.out.println("The password entered is incorrect");
        } else {
            System.out.println("Sorry, please enter the right format");
        }
    }
}

回答1:


The problem is that you call nextInt() in all ifs. This way you basically wait for another input each time you call passwordInput.nextInt().

Try to save the user input and then check it for the password. Something like:

if (passwordInput.hasNextInt()) {
    int pass = passwordInput.nextInt();
    if (pass == builtInPassword) {
        System.out.println("Your password is correct.");
    } else {
        System.out.println("The password entered is incorrect");
    }
} else {
    System.out.println("Sorry, please enter the right format");
}

I am writing without a compiler here so I am not sure it will compile properly but you can get the idea ;)




回答2:


scanner.hasNextInt() check the value is int or not, but its not consume the value. but in your code "pasword not matching" case scanner will go through two ifs and two hasNextInt() callings. therefore in second if it will return false value.

you can correct and optimize you code using exceptions as follow.

try {
     if(passwordInput.nextInt()==builtInPassword){
         System.out.println("Your password is correct.");
     }else{
         System.out.println("The password entered is incorrect");
     }
 } catch (InputMismatchException e) {
     System.out.println("Sorry, please enter the right format");        
 }



回答3:


As noted by others, the problem is that you are calling nextInt() twice, and it tries to get a new int each time. The simplest solution would be to change your first else to else if (passwordInput.hasNextInt()) {, as you already know the second condition (the password is wrong) holds from the fact that the if failed. However, I would advise restructuring your code so that there is no need to call hasNextInt twice either, as this seems cleaner:

if (passwordInput.hasNextInt()) {
    if (passwordInput.nextInt() == builtInPassword) {
        System.out.println("Your password is correct.");
    } else {
        System.out.println("The password entered is incorrect.");
    }
} else {
    System.out.println("Sorry, please enter the right format");
}



回答4:


I you need a while loop.

while (passwordInput.nextInt() != builtInPassword) {
   System.out.println("Incorrect password");
}
System.out.println("Correct password!");


来源:https://stackoverflow.com/questions/50598443/how-to-solve-the-else-if-problems-in-password-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!