[英]How would I write this code without using the && or the || operator?
public static double calculateMinPayment(double newBalance) {
double x;
if (newBalance < 0.00) {
return 0.00;
} else if (newBalance > 0.00 && newBalance <= 49.99) {
x = newBalance;
} else if (newBalance >= 50.00 && newBalance <= 300) {
x = 50.00;
} else {
x = newBalance * 0.2;
}
return x;
}
I'm not able to use the operators && or ||, how would I be able to format this same code without those operators?我无法使用运算符 && 或 ||,如果没有这些运算符,我将如何格式化相同的代码?
Since your conditions are in increasing size and with no gaps you don't need any AND conditions and the comparison can be made quite straight forward.由于您的条件在不断增加并且没有间隙,因此您不需要任何 AND 条件并且可以非常直接地进行比较。
public static double calculateMinPayment(double newBalance) {
double x;
if (newBalance < 0.0) {
return 0.0;
} else if (newBalance < 50.0) {
return newBalance;
} else if (newBalance <= 300.0) {
return 50.0;
} else {
return newBalance * 0.2;
}
}
A small variation is to skip all the else
since we are using return
一个小的变化是跳过所有
else
的,因为我们正在使用return
if (newBalance < 0.0) {
return 0.0;
}
if (newBalance < 50.0) {
return newBalance;
}
if (newBalance <= 300.0) {
return 50.0;
}
return newBalance * 0.2;
You can use Nested
if
statements like below:您可以使用嵌套的
if
语句,如下所示:
public static double calculateMinPayment(double newBalance) {
double x;
if (newBalance < 0.00) {
return 0.00;
} else if (newBalance > 0.00) {
if (newBalance <= 49.99) {
x = newBalance;
} else if (newBalance >= 50.00) {
if (newBalance <= 300) {
x = 50.00;
} else {
x = newBalance * 0.2;
}
} else {
x = newBalance * 0.2;
}
} else {
x = newBalance * 0.2;
}
return x;
}
To avoid writing if
totally, you could do the below.为避免写
if
totally,您可以执行以下操作。 The LinkedHashMap
keeps track of all conditions and the result to return if the condition is true
. LinkedHashMap
跟踪所有条件以及条件为true
时要返回的结果。 Since it is a LinkedHashMap
the order of conditions evaluated is the order in which they are put in the map, similar to the order if you were to write if
loops.由于它是一个
LinkedHashMap
,因此评估的条件顺序是它们在 map 中的顺序,类似于您编写if
循环时的顺序。
The logic returns the result of the first condition that evaluates to true
该逻辑返回第一个条件的结果,该条件的计算结果为
true
Any new condition can be simply added in the LinkedHashMap
, at the position equivalent to the order in which you want it to be evaluated任何新条件都可以简单地添加到
LinkedHashMap
中,在 position 相当于您希望它被评估的顺序
private static double calculateMinPayment(final double newBalance) {
LinkedHashMap<Predicate<Double>, Function<Double, Double>> conditionResult = new LinkedHashMap<>();
conditionResult.put(balance -> balance < 0.0, balance -> 0.0);
conditionResult.put(balance -> balance < 50.0, balance -> balance);
conditionResult.put(balance -> balance < 300.0, balance -> 50.0);
double minPayment = conditionResult.entrySet()
.stream()
.filter(entry -> entry.getKey().test(newBalance))
.findFirst()
.map(entry -> entry.getValue().apply(newBalance))
.orElse(newBalance * 0.2);
return minPayment;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.