首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果条件被忽略

问如果条件被忽略
EN

Stack Overflow用户
提问于 2019-08-01 06:18:38
回答 2查看 63关注 0票数 3

这段代码应该检查密码长度是否好,但如果密码长度大于或等于16,则跳过if条件,而不打印句子。

代码语言:javascript
复制
/* 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,则代码应输出“大密码长度”,但如果密码长度大于或等于16,则不会输出任何内容

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-01 06:23:08

if(Password.length() >= 8)和else if(Password.length() < 8)覆盖所有可能的密码长度,因此永远不会达到以下条件。

你应该以一种不那么令人困惑的方式来组织你的条件:

代码语言:javascript
复制
if (Password.length() < 8) {
    System.out.println("Bruv youre asking to be hacked");
} else if (Password.length() >= 8 && Password.length() <= 10) {
    System.out.println("Medium length of password");
} else if (Password.length() > 10 and Password.length() <= 13) {
    System.out.println("Good password length");
} else if (Password.length() > 13 && Password.length() < 16) {
    ... // you might want to output something for passwords of length between 13 and 16
} else {
    System.out.println("Great password length");
}

甚至更好

代码语言:javascript
复制
if (Password.length() < 8) {
    System.out.println("Bruv youre asking to be hacked");
} else if (Password.length() <= 10) {
    System.out.println("Medium length of password");
} else if (Password.length() <= 13) {
    System.out.println("Good password length");
} else if (Password.length() < 16) {
    ... // you might want to output something for passwords of length between 13 and 16
} else {
    System.out.println("Great password length");
}
票数 3
EN

Stack Overflow用户

发布于 2019-08-01 06:28:12

试着使用:

代码语言:javascript
复制
if (Password.length() >= 8) {
    if (Password.length() <= 10) {
        System.out.println("Medium length of password");
    } else if (Password.length() <= 13) {
        System.out.println("Good password length");
    } else if (Password.length() >= 16) {
        System.out.println("Great password length");
    }
} else if (Password.length() < 8) {
    System.out.println("Bruv youre asking to be hacked");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57302856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档