Как исправить, если и еще, если заявления не выбирая правильное утверждение - PullRequest
1 голос
/ 24 марта 2019

Я делаю калькулятор Box, который берет ввод ширины длины и высоты, а затем вычисляет их, сравнивает их и говорит что-то вроде: «Первое поле немного больше, чем следующее», и наоборот.Проблема состоит в том, что у меня есть несколько утверждений, которые говорят, что если размер поля в два раза в три раза выше, но он просто видит, что поле один больше, чем поле два, и отображает его.умножение

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 голосов
/ 24 марта 2019

Вам необходимо изменить порядок if с.Сначала вам нужно проверить количество
firstBox.calcVolume() / secondBox.calcVolume() и secondBox.calcVolume() / firstBox.calcVolume(), и если они больше 4, используйте эти if с, иначе используйте первый набор if с в обратном порядке.

Проблема с вашим кодом состоит в том, что первые условия уже выполняются, когда выполняются ваши последние условия, поэтому они никогда не будут выполнены.Я имею в виду, когда 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");
} 
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...