询问if语句

时间:2013-10-02 16:07:52

标签: java if-statement boolean-expression

public String toString() {

String details = number + ", " + RACE_DESCRIPTIONS[typeIndex] + "[" + date + "]" + ":";
if (!hasFinished) {
  details += "Race not finished";
} else if  (hasFinished = true) {
  details += "\n     1st: " + competitors[0].getName();
  details += "\n     2nd: " + competitors[1].getName();
  details += "\n     3rd: " + competitors[2].getName();
} else if {
  details += "n/a"; 
}
return details;

如果尚未设置位置,则应返回字符串文字“n / a”而不是该人的姓名。 - 我试图写一个IF语句,如果第1,第2和第3不是真的,而是返回n / a。

4 个答案:

答案 0 :(得分:1)

请勿使用=运算符来比较boolean值 - 即赋值运算符,因此始终为true。它已经boolean,只需使用:

} else if (hasFinished) {

此外,无论是false还是true,都不需要第三种情况。你可以删除

else if {
  details += "n/a"; 
}
无论如何,

在语法上是不正确的。

答案 1 :(得分:0)

=用于分配,==用于比较。在以下语句中使用==代替=

else if  (hasFinished = true)

这实际上会将hasFinished值赋值为true,并且条件也将始终计算为true。将其更改为:

else if  (hasFinished == true)

进行比较而不是分配

答案 2 :(得分:0)

由于两个原因,您永远不会执行details += "n/a"

  1. 您在hasFinished中为true分配了if(正如其他人所说):

    else if  (hasFinished = true)
    

    应该是

    else if  (hasFinished)
    
  2. 最重要的是,您的代码具有以下形式:

    if (false) {
        ...
    } else {
        if (true) {
        } else {
            //this is false, it was supposed to be hitted before
        }
    }
    
  3. 所以即使你做了第一次修复,背后的逻辑也没有任何意义。您应该在第二个if中使用另一个评估来处理最后一个else语句。例如:

    if (!hasFinished) {
        ...
    } else {
        //the hasFinished usage here has no sense, being commented
        //just left it to show this is supported
        //if (hasFinished && someValue <= someOtherValue) {
        if (someValue <= someOtherValue) {
            ...
        } else {
            ...
        }
    }
    

答案 3 :(得分:0)

基本上,你需要检查一下你是否真的有三个终结者,这就构成了你的else-if子句。

public String toString()
{
    String details = number + ", " + RACE_DESCRIPTIONS[typeIndex] + "[" + date + "]" + ":";
    if(!hasFinished) // The race has not yet finished.
    {
        details += "Race not finished";
    }
    else if(competitors.length >= 3) // Implies that hasFinished evaluated to true
    {
        details += "\n     1st: " + competitors[0].getName();
        details += "\n     2nd: " + competitors[1].getName();
        details += "\n     3rd: " + competitors[2].getName();
    }
    else // The race has finished, but there are less than three competitors
    {
        details += "n/a";
    }
    return details;
}

如果你想允许少于三个竞争者,但仍然显示第一/第二/第三,你可以使用三元比较并用else替换你的else-if子句。

public String toString()
{
    String details = number + ", " + RACE_DESCRIPTIONS[typeIndex] + "[" + date + "]" + ":";
    if(!hasFinished)
    {
        details += "Race not finished";
    }
    else // Implies that hasFinished evaluated to true
    {
        details += "\n     1st: " + (competitors.length >= 1 ? competitors[0].getName() : "n/a");
        details += "\n     2nd: " + (competitors.length >= 2 ? competitors[1].getName() : "n/a");
        details += "\n     3rd: " + (competitors.length >= 3 ? competitors[2].getName() : "n/a");
    }
    return details;
}