【问题标题】:Can you show me an alternative to these lengthy if else statements?你能告诉我这些冗长的 if else 语句的替代方法吗?
【发布时间】:2022-01-19 02:22:05
【问题描述】:

我正在尝试创建代码来获取客户购买的商品数量,然后打印出将打折的数量。

1-3 items purchased will get no discount
4-6 items purchased will get 5% discount
7-10 items purchased will get 10% discount
11 or more items purchased will get a 15% discount

任何帮助将不胜感激,谢谢。这是我创建的代码。

package discount;
import java.util.Scanner;

public class Discount {

    public static void main(String[] args) {    
        Scanner input = new Scanner(System.in);
        System.out.println("Input the number of items: ");
        int quantity = input.nextInt();
                
        int[] discount = {0, 5, 10, 15};
        
        if (quantity >= 1 && quantity < 4) {
            System.out.print("no discount");
        } else if (quantity >= 4 && quantity < 7) {
            System.out.print(discount[1] + "% discount");
        } else if (quantity >= 7 && quantity < 11) {
            System.out.print(discount[2] + "% discount");
        } else if (quantity >= 11) {
            System.out.print(discount[3] + "% discount");
        }     
    }
}

【问题讨论】:

  • 在这种情况下,switch 语句会起作用。见docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html。如果要枚举的案例标签太多,那么TreeMap 类是一个替代方案。
  • 我同意上面的说法,但上面的代码还不是我归类为“冗长”的代码。格式不正确,但我可以为您解决。
  • 同意。值得怀疑的是是否值得努力缩短这段代码。
  • 感谢您的评论!你能教我如何使它格式化吗?
  • 一种简化方法是删除 discount 数组,并在 if 语句的主体中使用文字。这样做的好处是您可以立即看到与哪个折扣相关联的数量,并消除了在 if 语句中存在数组索引的可能性,而该索引在 discount 中不存在。但是,此更改与学习 Java 的目标相冲突,我希望您在处理此代码时确实获得并修复了至少一个 ArrayIndexOutOfBoundsException

标签: java arrays if-statement range


【解决方案1】:

我建议您通过将负责计算折扣和打印消息的代码提取到单独的方法中来应用分解。即使没有 switch 表达式,它也会使您的代码更清晰。

1.计算折扣:

public static int getDiscount(int items) {
    return switch(items) {
        case 0, 1, 2, 3 -> 0;
        case 4, 5, 6 -> 5;
        case 7, 8, 9, 10 -> 10;
        default -> 15;
    };
}

2。打印消息:

public static void printMessage(int discount) {
    switch(discount) {
        case 0 -> System.out.print("no discount");
        default -> System.out.println(discount + "% discount");
    }
}

3.主要:

public static void main(String[] args) {    
    Scanner input = new Scanner(System.in);
    System.out.println("Input the number of items: ");
            
    printMessage(getDiscount(input.nextInt()));
}
  • 此代码适用于 Java 14 及更高版本

【讨论】:

  • 这不适用于 Java 11 或更早版本。 (或在 Android 上)。 Switch 表达式是一种新的语言功能...
  • 没错,它只适用于最新的 LTS 版本的 Java
【解决方案2】:

如果没有 Kotlin when,Java 的 switch 可能会也可能不会做任何你想做的事情。但是如果你确实想用if-else 写,它可以清理:

if(quantity > 0){ 
    int discount = -1
    if(quantity < 4) discount = 0; 
    else if (quantity < 7) discount = 5; 
    else if (quantity < 11) discount = 10;
    else discount = 15; 
    printDiscountMessage(discount)
}

【讨论】:

    【解决方案3】:

    使用这个

    int discountCalculation = (quantity >= 1 && quantity < 4) ?discount[0]: (quantity >= 4 && quantity < 7)?discount[1]: (quantity >= 7 && quantity < 11) ?discount[2]:discount[3];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      相关资源
      最近更新 更多