if Scanner Boolean statement with 2 nextInt()

我写了这段代码,但是当我的密码正确时我有问题。请指导我。我是 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!");

        }


    }


}


郎朗坤
浏览 119回答 1
1回答

慕码人8056858

您必须调用一次才能获取密码,如下所示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!");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java