我想知道是否有人可以帮助我理解如何从这种方法中消除两个成员的布尔值:
public boolean testAdd() {
boolean addPositSuccess;
boolean addNegatSuccess;
Double firstPositiveNumber = 22.0;
Double secondPositiveNumber = 33.0;
calc.x = firstPositiveNumber;
if (calc.x(secondPositiveNumber) == (firstPositiveNumber + secondPositiveNumber)) {
System.out.println("[ OK ] Calculator can add positive numbers");
addPositSuccess = true;
} else {
System.out.println("[FAIL] Calculator adds incorrectly");
addPositSuccess = false;
}
Double firstNegativeNumber = -5.0;
Double secondNegativeNumber = -6.0;
calc.x = firstNegativeNumber;
if (calc.x(secondNegativeNumber) == (firstNegativeNumber + secondNegativeNumber)) {
System.out.println("[ OK ] Calculator can add a negative number");
addNegatSuccess = true;
} else {
System.out.println("[FAIL] Calculator adds with negative numbers incorrectly");
addNegatSuccess = false;
}
if ((addPositSuccess = true) && (addNegatSuccess = true)) {
return true;
} else {
return false;
}
}
让我们假设calc只是指一个存根类的实例。
答案 0 :(得分:0)
也许您可以简单地将Double对象用于此目的。尽管如此,它非常难看,我真的很想知道为什么(以及如何)要消除布尔值。你确定你的方式正确吗?
public boolean testAdd() {
Double firstPositiveNumber = 22.0;
Double secondPositiveNumber = 33.0;
calc.x = firstPositiveNumber;
if (calc.x(secondPositiveNumber) == (firstPositiveNumber + secondPositiveNumber)) {
System.out.println("[ OK ] Calculator can add positive numbers");
// addPositSuccess = true;
} else {
System.out.println("[FAIL] Calculator adds incorrectly");
firstPositiveNumber = null;
}
Double firstNegativeNumber = -5.0;
Double secondNegativeNumber = -6.0;
calc.x = firstNegativeNumber;
if (calc.x(secondNegativeNumber) == (firstNegativeNumber + secondNegativeNumber)) {
System.out.println("[ OK ] Calculator can add a negative number");
// addNegatSuccess = true;
} else {
System.out.println("[FAIL] Calculator adds with negative numbers incorrectly");
firstNegativeNumber = null;
}
if ((firstPositiveNumber != null) && (firstNegativeNumber != null)) {
return true;
} else {
return false;
}
}
答案 1 :(得分:0)
if
表达式中的任何语句都返回一个布尔值;因此,而不是写作:
if (condition)
someBoolean = true;
else
someBoolean = false;
你可以写:
someBoolean = condition;
将此应用于您的代码,您将大大缩短它...向左运动。
答案 2 :(得分:0)
试试这个:
public boolean testAdd() {
Double firstPositiveNumber = 22.0;
Double secondPositiveNumber = 33.0;
calc.x = firstPositiveNumber;
if (calc.x(secondPositiveNumber) == (firstPositiveNumber + secondPositiveNumber)) {
System.out.println("[ OK ] Calculator can add positive numbers");
return true;
} else {
System.out.println("[FAIL] Calculator adds incorrectly");
return false;
}
Double firstNegativeNumber = -5.0;
Double secondNegativeNumber = -6.0;
calc.x = firstNegativeNumber;
if (calc.x(secondNegativeNumber) == (firstNegativeNumber + secondNegativeNumber)) {
System.out.println("[ OK ] Calculator can add a negative number");
return true;
} else {
System.out.println("[FAIL] Calculator adds with negative numbers incorrectly");
return false;
}
}
答案 3 :(得分:0)
请注意,您的测试几乎完全相同。因此,您通常会将这些因素纳入方法:
public boolean testAdd() {
// note: using & instead of && ensures both methods are called
return testAddPositive() & testAddNegative();
}
private boolean testAddPositive() {
return testAdd(22.0, 33.0, "positive");
}
private boolean testAddNegative() {
return testAdd(-5.0, -6.0, "negative");
}
private boolean testAdd(Double firstNumber, Double secondNumber, String type) {
calc.x = firstNumber;
if (calc.x(secondNumber) == (firstNumber + secondNumber)) {
System.out.println("[ OK ] Calculator can add " + type + " numbers");
return true;
} else {
System.out.println("[FAIL] Calculator adds " + type + " incorrectly");
return false;
}
}