简体   繁体   English

如果不使用 && 或 ||,我将如何编写这段代码? 操作员?

[英]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.

相关问题 我将如何为此代码编写 JUnit 测试? - How would I write a JUnit test for this code? 我将如何在 java 中编写此 python 代码 - How would i write this python code in java 如何在不使用运算符的情况下编写LessThan方法 - How to write a LessThan method without using the operator 我可以在不使用序列化的情况下编写此代码吗? - Can I write this code without using serialization? 您将如何使用 Java Stream API 编写此代码? 与此代码相比,优势是什么? - How would you write this code using the Java Stream API? And what would be the advantage compared to this code? 这段代码怎么写? : Java 中的运算符 - how to write this code in ? : operator in Java 如何在不使用“ if-else if”的情况下编写针对不同参数使用不同方法的代码? - How can I write code that uses a different method for different parameters without using “if-else if”? 使用列表,我如何像使用列表一样更改此代码 - Using List, how would I change this code as using List 如何使用条件(三元)运算符编写以下代码? - How to write following code with Conditional(Ternary) Operator? 如何在没有循环的情况下仅使用 foldLeft 获得 minVal? - How would I get the minVal without a loop and using only foldLeft?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM