我写这段代码,但是当我的密码正确时我有问题。请指导我。我是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!");
}
}
}
最佳答案
您只需调用一次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!");
}