小葡萄、 2024-06-18 17:48 采纳率: 11.1%
浏览 24
已结题

java 判断某个数 区间是否存在

img

如果上面需求 我想添加到数据库里面 需要4个字段 20, <=, < ,30 ,(类似20<=x<30) 其中运算符的话 可以出现2个 也可以出现1次 比如 70, > 这种 ,类似 x>70 现在添加的时候 我怎么判断我新添加的是否在这个区间。比如 我现在添加一个 <= 20 那么 不能添加了 因为20已经存在了 但是我要添加< 20 那么可以添加 大概这个意思 我现在判断加判断 首先运算符有5个 我要判断5次 然后还要在判断保存到数据库的值 是大于还是小于等。然后在比较这个 比如 25<=x<55 的 我要先拿25到数据库看 有没有大于等于25的 然后还有判断数据库 25这个运算符是什么 如果原来有个x<25 那么我可以添加 但是 如果是x<=25 那么我还不能添加 后面55也要判断 就是逻辑写的很多多 有什么简便方法呀

  • 写回答

20条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-06-18 17:48
    关注
    获得0.45元问题酬金

    引自免费微信小程序:皆我百晓生

    为了简化这个问题并构建一个高效的解决方案,我们可以设计一个实体类GrossMarginRule来存储毛利率规则,其中包含边界值和边界类型(开区间或闭区间)。然后,利用这些信息,我们可以在添加新规则时检查与现有规则的冲突。

    首先,定义GrossMarginRule实体类:

    public class GrossMarginRule {
        private double lowerBound;
        private boolean lowerInclusive; // true for '[', false for '('
        private double upperBound;
        private boolean upperInclusive; // true for ']', false for ')'
    
        public GrossMarginRule(double lowerBound, boolean lowerInclusive, double upperBound, boolean upperInclusive) {
            this.lowerBound = lowerBound;
            this.lowerInclusive = lowerInclusive;
            this.upperBound = upperBound;
            this.upperInclusive = upperInclusive;
        }
    
        // Getters and setters
        public double getLowerBound() { return lowerBound; }
        public boolean isLowerInclusive() { return lowerInclusive; }
        public double getUpperBound() { return upperBound; }
        public boolean isUpperInclusive() { return upperInclusive; }
    
        // Check if a value fits within the rule's range
        public boolean isValueInRange(double value) {
            if (lowerInclusive) {
                if (upperInclusive) {
                    return value >= lowerBound && value <= upperBound;
                } else {
                    return value >= lowerBound && value < upperBound;
                }
            } else {
                if (upperInclusive) {
                    return value > lowerBound && value <= upperBound;
                } else {
                    return value > lowerBound && value < upperBound;
                }
            }
        }
    
        // Check if another rule overlaps with this rule
        public boolean doesOverlap(GrossMarginRule other) {
            return this.isValueInRange(other.getLowerBound()) || this.isValueInRange(other.getUpperBound())
                   || other.isValueInRange(this.getLowerBound()) || other.isValueInRange(this.getUpperBound());
        }
    }
    

    接下来,创建一个GrossMarginRuleManager类,用于管理规则列表,并提供添加规则的方法,同时检查是否与已有规则冲突:

    import java.util.List;
    import java.util.ArrayList;
    
    public class GrossMarginRuleManager {
        private List<GrossMarginRule> rules;
    
        public GrossMarginRuleManager() {
            this.rules = new ArrayList<>();
        }
    
        public void addRule(GrossMarginRule newRule) throws Exception {
            for (GrossMarginRule existingRule : rules) {
                if (newRule.doesOverlap(existingRule)) {
                    throw new Exception("New rule overlaps with an existing rule.");
                }
            }
            rules.add(newRule);
        }
    
        // Other methods like getting all rules, removing rules etc.
    }
    

    使用示例:

    public class Main {
        public static void main(String[] args) {
            GrossMarginRuleManager manager = new GrossMarginRuleManager();
            try {
                manager.addRule(new GrossMarginRule(20, true, 30, false));
                // Attempt to add a conflicting rule
                manager.addRule(new GrossMarginRule(20, false, 25, true)); // This should not throw an exception
                manager.addRule(new GrossMarginRule(20, true, 25, true)); // This should throw an exception
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    

    通过这种方式,你不仅能够清晰地管理毛利率规则,还能够有效地检测和防止规则间的冲突。这种方法减少了需要编写的逻辑判断量,并提高了代码的可读性和维护性。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月26日
  • 创建了问题 6月18日