我写了这段代码,但是当我的密码正确时我遇到了问题。请指导我。我是java编码的新手
import java.util.Scanner;
public class PasswordProjectQ {
private static Scanner passwordInput;
public static void main(String[] args) {
passwordInput = new Scanner(System.in);
int builtInPassword = 3720118;
System.out.println("Please enter your password:(just integer)");
if(passwordInput.hasNextInt() && passwordInput.nextInt() != builtInPassword) {
System.out.println("You entered the right format \nbut the password is WRONG!");
}else if(passwordInput.hasNextInt() && passwordInput.nextInt() == builtInPassword) {
System.out.println("Thanks,your password is correct");
}else {
System.out.println("WRONG format!");
}
}
}
答案 0 :(得分:1)
您只需调用一次hasNextInt()
和passwordInput.nextInt()
即可获得密码,如下所示:
if (passwordInput.hasNextInt()) {
if (passwordInput.nextInt() != builtInPassword) {
System.out.println("You entered the right format \nbut the password is WRONG!");
} else {
System.out.println("Thanks,your password is correct");
}
} else {
System.out.println("WRONG format!");
}