Java方法从if / else打印两个选项

时间:2013-09-25 07:37:48

标签: java if-statement printing

嗨,这是我为一个项目构建的一些代码,它调用了其他几种方法来打印关于此人的声明。我的问题是它以double形式打印并且似乎正在运行if / else语句。

我该怎样防止这种情况?如果需要,我可以从我的终端添加文本。代码如下;

 void printPersonDescription(String name, boolean sex, int age) {

    if(sex == true && yearsUntilNextMaturityLevel(age) > 1) {
        println (name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
        " she has " + yearsUntilNextMaturityLevel(age) + " years left until she is " + 
        nextMaturityLevel(age) + "."); 
    }
    else if(sex == false && yearsUntilNextMaturityLevel(age) > 1) {
        println (name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
        " he has " + yearsUntilNextMaturityLevel(age) + " years left until he is " + 
        nextMaturityLevel(age) + ".");
    }
    if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {
        println (name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
        " she has " + yearsUntilNextMaturityLevel(age) + " year left until she is " + 
        nextMaturityLevel(age) + "."); 
    }
    else {
        println (name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
        " he has " + yearsUntilNextMaturityLevel(age) + " year left until he is " + 
        nextMaturityLevel(age) + ".");
    }

}

3 个答案:

答案 0 :(得分:4)

请注意您的四个案例有多相似。您只是更改了打印行中的某些单词。这四个if / else块可以简化为:

void printPersonDescription(String name, boolean sex, int age) {
    int years = yearsUntilNextMaturityLevel(age);
    String gender = sex ? "she" : "he";
    String plural = years > 1 ? "s" : "";
    println(name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
    " " + gender + " has " + years + " year" + plural + " left until " + gender + " is " + 
    nextMaturityLevel(age) + "."); 
}

此外,您可以使用println(假设成熟度级别为字符串)使String.format更具可读性

String.format("%s is %s. At %d %s has %d year%s left until %s is %s.", 
        name, getMaturityLevel(age), age, gender, years, plural, gender, 
        nextMaturityLevel(age))

答案 1 :(得分:2)

代码中的第三个if条件应为else if,以避免双重打印。

而不是:

if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {

使用:

else if (sex && yearsUntilNextMaturityLevel(age) <= 1) {

顺便说一下,您不需要使用if (boolVar == true)即可使用if (boolVar)

答案 2 :(得分:0)

 void printPersonDescription(String name, boolean sex, int age) {

    if(sex == true && yearsUntilNextMaturityLevel(age) > 1) {
        println (name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
        " she has " + yearsUntilNextMaturityLevel(age) + " years left until she is " + 
        nextMaturityLevel(age) + "."); 
    }
    else if(sex == false && yearsUntilNextMaturityLevel(age) > 1) {
        println (name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
        " he has " + yearsUntilNextMaturityLevel(age) + " years left until he is " + 
        nextMaturityLevel(age) + ".");
    }
    else if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {    
        println (name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
        " she has " + yearsUntilNextMaturityLevel(age) + " year left until she is " + 
        nextMaturityLevel(age) + "."); 
    }
    else {
        println (name + " is " + getMaturityLevel(age) + ". " + "At " + age + 
        " he has " + yearsUntilNextMaturityLevel(age) + " year left until he is " + 
        nextMaturityLevel(age) + ".");
    }

}

您需要在第三个else之前添加if。否则,最终else语句将匹配第三个if,因此,else语句意味着

if(sex != true || yearsUntilNextMaturityLevel(age) > 1)