if 条件被忽略

这段代码应该检查密码长度是否合适,但如果密码长度大于或等于 16,它会跳过 if 条件并且不打印句子。


/* This bit of the coe just checks the length of the password */

if (Password.length() >= 8) {

    if (Password.length() <= 10) {

        System.out.println("Medium length of password");

    }

}

else if (Password.length() < 8) {

    System.out.println("Bruv youre asking to be hacked");

else if (i >= 10) {

    if (Password.length() <= 13) {

        System.out.println("Good password length");

    }

    /* The part that fails */

    else if (Password.length() >= 16) {

        System.out.println("Great password length");

    }        

如果密码长度大于或等于 16,则代码应输出“Great password length”,但如果密码长度大于或等于 16,则不会输出任何内容


互换的青春
浏览 123回答 2
2回答

汪汪一只猫

if(Password.length() >= 8)并else if(Password.length() < 8)覆盖所有可能的密码长度,因此永远不会达到以下条件。你应该以一种不那么混乱的方式组织你的条件:if (Password.length() < 8) {&nbsp; &nbsp; System.out.println("Bruv youre asking to be hacked");} else if (Password.length() >= 8 && Password.length() <= 10) {&nbsp; &nbsp; System.out.println("Medium length of password");} else if (Password.length() > 10 and Password.length() <= 13) {&nbsp; &nbsp; System.out.println("Good password length");} else if (Password.length() > 13 && Password.length() < 16) {&nbsp; &nbsp; ... // you might want to output something for passwords of length between 13 and 16} else {&nbsp; &nbsp; System.out.println("Great password length");}甚至更好if (Password.length() < 8) {&nbsp; &nbsp; System.out.println("Bruv youre asking to be hacked");} else if (Password.length() <= 10) {&nbsp; &nbsp; System.out.println("Medium length of password");} else if (Password.length() <= 13) {&nbsp; &nbsp; System.out.println("Good password length");} else if (Password.length() < 16) {&nbsp; &nbsp; ... // you might want to output something for passwords of length between 13 and 16} else {&nbsp; &nbsp; System.out.println("Great password length");}

婷婷同学_

尝试使用:if (Password.length() >= 8) {&nbsp; &nbsp; if (Password.length() <= 10) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Medium length of password");&nbsp; &nbsp; } else if (Password.length() <= 13) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Good password length");&nbsp; &nbsp; } else if (Password.length() >= 16) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Great password length");&nbsp; &nbsp; }} else if (Password.length() < 8) {&nbsp; &nbsp; System.out.println("Bruv youre asking to be hacked");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java