【发布时间】:2015-07-20 20:52:58
【问题描述】:
问题: 如何读取字符串“d6+2-d4”,以便每个 d# 在掷骰子的参数内随机生成一个数字?
澄清器:
我想读取一个字符串并拥有它,因此当出现 d# 时,它将随机生成一个数字,例如模拟掷骰子。然后,将所有卷和数字相加得到总数。就像 Roll20 如何使用他们的 /roll 命令一样。 If !clarifying {lstThen.add("look at the Roll20 and play with the /roll command to understand it")} else if !understandStill {lstThen.add("I do not know what to say, someone else could try explaining it better...")}
信息: 我正在为 Dungeons and Dragons 制作一个 Java 程序,结果发现我在计算用户输入时遇到了一个问题:我不知道如何评估这样的字符串。
我推测我最后可能需要 Java 的 eval。我确实知道我想要发生什么/有一个关于如何执行的理论(这比 Java 更 PseudoCode):
Random rand = new Random();
int i = 0;
String toEval;
String char;
String roll = txtField.getText();
while (i<roll.length) {
check if character at i position is a d, then highlight the numbers
after d until it comes to a special character/!aNumber
// so if d was found before 100, it will then highlight 100 and stop
// if the character is a symbol or the end of the string
if d appears {
char = rand.nextInt(#);
i + #'s of places;
// so when i++ occurs, it will move past whatever d# was in case
// d# was something like d100, d12, or d5291
} else {
char = roll.length[i];
}
toEval = toEval + char;
i++;
}
perform evaluation method on toEval to get a resulting number
list.add(roll + " = " + evaluated toEval);
编辑: 在韦斯顿的帮助下,我已经了解了可能需要什么,使用带有数组的拆分器,它可以检测某些符号并将其添加到列表中。但是,我没有说明还需要什么是我的错。上面的伪代码没有帮助,所以这是我需要弄清楚的。
roll.split("(+-/*^)");
因为这部分也是让我绊倒的地方。我也应该在有数字的地方进行拆分吗?所以一个等式:
String[] numbers = roll.split("(+-/*^)");
String[] symbols = roll.split("1234567890d")
// Rough idea for long way
loop statement {
loop to check for parentheses {
set operation to be done first
}
if symbol {
loop for symbol check {
perform operations
}}} // ending this since it looks like a bad way to do it...
// Better idea, originally thought up today (5/11/15)
int val[];
int re = 1;
loop {
if (list[i].containsIgnoreCase(d)) {
val[]=list[i].splitIgnoreCase("d");
list[i] = 0;
while (re <= val[0]) {
list[i] = list[i] + (rand.nextInt(val[1]) + 1);
re++;
}
}
}
// then create a string out of list[]/numbers[] and put together with
// symbols[] and use Java's evaluator for the String
wenton 拥有它,它似乎不是为我做的(直到我意识到我并没有具体说明我想要什么)所以基本上更新,我想要评估的字符串是(我知道它有点非正统的,但这是为了说明一点;我也希望这能进一步阐明使其发挥作用所需的条件):
(3d12^d2-2)+d4(2*d4/d2)
通过阅读本文,您可能会看到我不知道如何表现得很好的地方......但这就是为什么我要问你们所有可爱的聪明程序员!我希望我问的足够清楚,感谢您抽出宝贵时间:3
【问题讨论】:
-
你的目标不是 100% 明确的,能否请你给出一个完整的例子来说明你想要什么,这样它会让你的目标明确吗?
-
问一个问题然后彻底改变它是不公平的堆栈溢出做法。没有人可以回答移动的球门柱。您现在有足够的信息来研究一些真正的 java 代码,伪代码就足够了!当你再次陷入困境时,再问一个问题。
-
好吧,我就不说了……
标签: java random eval jtextfield dice