【问题标题】:How to fix if and else if statements not choosing the right statement如何修复 if 和 else if 语句没有选择正确的语句
【发布时间】:2019-03-24 01:15:53
【问题描述】:

我正在做一个盒子计算器,它接受长度宽度和高度的输入,然后计算它并比较两者,然后说“第一个盒子比下一个盒子稍大”,反之亦然。问题是我有多个语句说如果盒子是大小的两倍三四倍,但它只是看到盒子一大于盒子二并显示它。

我已经尝试了所有的 if 语句除法或乘法

public void calculateSizes() {
    firstBox.calcVolume();
    secondBox.calcVolume();

    if (firstBox.calcVolume() == secondBox.calcVolume()) {   //Calculate the size difference and display the corresponding message
        message = ("The first box is the same size as the second box");
    }else if (firstBox.calcVolume() > secondBox.calcVolume() ) {
        message = ("The first box is slightly bigger than the second box");

    }else if (secondBox.calcVolume() > firstBox.calcVolume()) {
        message = ("The second box is slightly bigger than the first box");

    }else if (firstBox.calcVolume() >= secondBox.calcVolume() / 2) {
        message = ("The first box is twice the size than the second box");

    }else if (secondBox.calcVolume() >= firstBox.calcVolume() / 2) {
        message = ("The second box is twice the size than the first box");

    }else if (firstBox.calcVolume() >= secondBox.calcVolume() / 3) {
        message = ("The first box is triple the size than the second box");

    }else if (secondBox.calcVolume() >= firstBox.calcVolume() / 3) {
        message = ("The second box is triple the size than the first box");

    }else if (firstBox.calcVolume() >= secondBox.calcVolume() / 4) {
        message = ("The first box is quadruple the size than the second box");

    }else if (secondBox.calcVolume() >= firstBox.calcVolume() / 4) {
        message = ("The second box is quadruple the size than the first box");

    }else if (firstBox.calcVolume() == firstBox.calcVolume() / secondBox.calcVolume()) {
        message =("\n Box one is " + firstBox.calcVolume() / secondBox.calcVolume() + " times the size second box" );

    }else if (secondBox.calcVolume() == secondBox.calcVolume() / firstBox.calcVolume()) {  
        message =("\n Box two is " + secondBox.calcVolume() / firstBox.calcVolume() + " times the size first box");
    }
}

【问题讨论】:

  • 刚刚编辑了我的答案,请运行它,看看是否有效,然后分配 firstBox.calcVolume();例如,如果该方法返回一个 int,则另一行类似于 int firstVolume = firstBox.calcVolume();

标签: java if-statement


【解决方案1】:

您需要颠倒 ifs 的顺序。首先,您需要检查
firstBox.calcVolume() / secondBox.calcVolume()secondBox.calcVolume() / firstBox.calcVolume() 的数量,如果它们大于4,则使用ifs,否则按相反顺序使用第一组ifs。

您的代码的问题是,当您的最后一个条件为真时,第一个条件已经为真,因此它永远不会满足它们。我的意思是当a 数字是数字b 的四倍时,它也比它大:),所以会设置上层消息。

public void calculateSizes() {
//if you assign these values to variables it will be much better and you wont need to compute it all the time    
firstBox.calcVolume();
secondBox.calcVolume();


message = "\n ";
if (secondBox.calcVolume() > firstBox.calcVolume() &&  secondBox.calcVolume() / firstBox.calcVolume() > 4 ) {  
    message += ("Box two is " + (secondBox.calcVolume() / firstBox.calcVolume()) + " times the size first box");
} else if (firstBox.calcVolume() > secondBox.calcVolume() &&  firstBox.calcVolume() / secondBox.calcVolume() > 4 ) {  
    message += ("Box one is " + (firstBox.calcVolume() / secondBox.calcVolume()) + " times the size second box" ); 
} else if (firstBox.calcVolume() == secondBox.calcVolume()) { 
    message += ("The first box is the same size as the second box");
} else if (secondBox.calcVolume() >= firstBox.calcVolume() / 4) {
    message += ("The second box is quadruple the size than the first box");
} else if (firstBox.calcVolume() >= secondBox.calcVolume() / 4) {
    message += ("The first box is quadruple the size than the second box");
} else if (firstBox.calcVolume() >= secondBox.calcVolume() / 3) {
    message += ("The first box is triple the size than the second box");
} else if (secondBox.calcVolume() >= firstBox.calcVolume() / 3) {
    message += ("The second box is triple the size than the first box");
} else if (firstBox.calcVolume() >= secondBox.calcVolume() / 2) {
    message += ("The first box is twice the size than the second box");
} else if (secondBox.calcVolume() >= firstBox.calcVolume() / 2) {
    message += ("The second box is twice the size than the first box");
} else if (firstBox.calcVolume() > secondBox.calcVolume() ) {
    message += ("The first box is slightly bigger than the second box");
} 
 }

【讨论】: