我寫了這段代碼,但是當我的密碼正確時我有問題。請指導我。我是 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!"); } }}
1 回答

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
您必須調用一次才能獲取密碼,如下所示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!");
}
添加回答
舉報
0/150
提交
取消