我寫了這段代碼,但是當(dāng)我的密碼正確時(shí)我有問題。請指導(dǎo)我。我是 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貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
您必須調(diào)用一次才能獲取密碼,如下所示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!");
}
添加回答
舉報(bào)
0/150
提交
取消
