简体   繁体   中英

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

Im doing a Box calculator that takes the input of the length width and height and then calculates it and compares the two and says something like "The first box is slightly bigger then the next one" vice versa. The issue is I have multiple statements that say if the box is double triple quadruple the size but it just sees that box one is greater then box two and display it.

I have tried all if statements dividing the box or multiplying

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

You need to reverse the order of the if s . First you need to check the amount of
firstBox.calcVolume() / secondBox.calcVolume() and secondBox.calcVolume() / firstBox.calcVolume() and if they are bigger than 4 use those if s else use the first set of if s in reverse order.

The problem with your code is the first conditions are already true when your last conditions are, so it will never go to them. I mean when a number is four times bigger than number b , it is also bigger than it :), so upper messages will be set.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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