【发布时间】: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