【问题标题】:Regular expression help in Java dice rolling programJava掷骰子程序中的正则表达式帮助
【发布时间】:2016-09-26 22:02:16
【问题描述】:

我整天都在做这件事,我一辈子都无法弄清楚我做错了什么。该代码使我的 Android 模拟器崩溃。我正在尝试做的是编写一个掷骰子程序,我让它与作为单个实体滚动的骰子一起工作,但我正在尝试处理像 3d6 或 5d4 这样的掷骰子。对 singleRoll 的调用一次滚动一个骰子,我试图将更长的方程式分解为更简单的位...“3d4+5-13d6+7d8+9”

bbs.randInt 返回 [0,diceSize)。

public int multiPartRoll(String roll) {
        String[] parts = roll.split("(?=[+-])"); //split by +-, keeping them
        int total = 0;

        // TODO: Replace 5d4 with d4+d4+d4+d4+d4
        for (String partOfRoll : parts) { //roll each dice specified


            if (partOfRoll.matches("\\d+d\\d+")) {
                String[] splitString = (partOfRoll.split("d"));
                int times = Integer.getInteger(splitString[0]);
                int die = Integer.getInteger(splitString[1]);
                int i;
                for (i = 0; i < times; i++) {
                    String rollStr = "d" + die;
                    total += singleRoll(rollStr);
                }
            }
            else {
                total += singleRoll(partOfRoll);   
            }

        }
        return total;
    }

public int singleRoll(String roll) {
        int di = roll.indexOf('d');
        if (di == -1) //case where has no 'd'
            return Integer.parseInt(roll);
        int diceSize = Integer.parseInt(roll.substring(di + 1)); //value of string after 'd'
        int result = bbs.randInt(diceSize) + 1; //roll the dice
        if (roll.startsWith("-")) //negate if nessasary
            result = -result;
        return result;
    }

【问题讨论】:

  • 您遇到错误了吗?如果是这样,请发布堆栈跟踪。
  • singleRoll(String) 方法在哪里?您也可以发布该代码吗?
  • public int singleRoll(String roll) { int di = roll.indexOf('d'); if (di == -1) //case where has no 'd' return Integer.parseInt(roll); int diceSize = Integer.parseInt(roll.substring(di + 1)); //value of string after 'd' int result = bbs.randInt(diceSize) + 1; //roll the dice if (roll.startsWith("-")) //negate if nessasary result = -result; return result; }
  • 对不起,我是新人。我不知道如何在评论中发布代码。
  • 使用该代码编辑您的主帖。

标签: java calculator dice


【解决方案1】:

问题在于使用了错误的Integer解析方法。

尝试替换所有出现的

Integer.getInteger

Integer.parseInt

您使用的方法不会将 String 转换为 Integer:http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#getInteger%28java.lang.String%29

最好的问候。

【讨论】:

    【解决方案2】:

    介绍Dice接口:

    interface Dice {
    
        int roll();
    }
    

    还有两个类 NDice 是“普通骰子”:

    class NDice implements Dice {
    
        private final int nb;
        private final int sides;
        private Random r = new Random();
    
        NDice(String[] desc) {
            this.nb = Integer.parseInt(desc[0]);
            this.sides = Integer.parseInt(desc[1]);
        }
    
        @Override
        public int roll() {
            return nb < 0 ? -1 : 1 * IntStream.generate(() -> r.nextInt(sides) + 1).limit(Math.abs(nb)).sum();
        }
    }
    

    CDice 是“恒定骰子”:

    class CDice implements Dice {
        private int constant;
    
        public CDice(int constant) {
            this.constant = constant;
        }
    
        @Override
        public int roll() {
            return constant;
        }
    }
    

    然后我们可以引入将rollDescription解析成骰子集合并掷骰子的方法:

    static int roll(String rollDescription) {
        String[] parts = rollDescription.split("(?=[+-])");
        return Arrays.stream(parts)
                .map(s -> {
                    if (s.contains("d")) {
                        return new NDice(s.split("d"));
                    }
                    return new CDice(Integer.parseInt(s));
                }).map(Dice::roll).reduce(0, (a, b) -> a + b);
    }
    

    现在简短说明:

    • CDice我们只需要整合接口
    • 在表达式nb &lt; 0 ? -1 : 1 * ... Math.abs(nb) 中,我们需要在滚动数之前支持- (nb)。
    • 我省略了关于卷的元信息。如果您需要,您可以引入包含结果和一些关于骰子的额外信息的类 RollResult
    • 命名可能会更好
    • 静态方法roll需要'home class'。 Dice 界面不错,但不是完美的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      相关资源
      最近更新 更多