繁体   English   中英

如何解决if和else if语句未选择正确的语句

[英]How to fix if and else if statements not choosing the right statement

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

我已经尝试了所有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");
    }
}

您需要颠倒if s的顺序。 首先,您需要检查
firstBox.calcVolume() / secondBox.calcVolume()secondBox.calcVolume() / firstBox.calcVolume()并且如果它们大于4使用的那些if小号否则使用第一组if S IN相反的顺序。

您的代码的问题是,当您的最后一个条件满足时,第一个条件已经为真,因此永远不会使用它们。 我的意思是,当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");
} 
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM