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

时间:2019-03-24 01:15:53

标签: java if-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");
    }
}

1 个答案:

答案 0 :(得分:0)

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

您的代码存在的问题是,当您的最后一个条件成立时,第一个条件已经成立,因此永远不会解决这些问题。我的意思是,当a的数字是数字b的四倍时,它也比数字{:1)大,所以将设置高位消息。

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");
} 
 }