为了账号安全,请及时绑定邮箱和手机立即绑定

if Scanner Boolean statement with 2 nextInt()

if Scanner Boolean statement with 2 nextInt()

郎朗坤 2022-05-12 17:18:40
我写了这段代码,但是当我的密码正确时我有问题。请指导我。我是 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!");

}


查看完整回答
反对 回复 2022-05-12
  • 1 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号