问题: 如何读取字符串" 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...")}
的信息: 我正在为“龙与地下城”制作一个Java程序,却发现我在弄清楚如何计算用户输入方面遇到了一个问题:我不知道如何评估这样的字符串。
我认为我最后可能需要Java eval
。我确实知道我想要发生什么/有关于如何执行的理论(这比 PseudoCode 更像是Java):
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);
编辑: 在Weston的帮助下,我已经磨练了可能需要的东西,使用带阵列的分离器,它可以检测某些符号并将其添加到列表中。但是,我没有澄清需要什么。上面的伪代码并没有帮助,所以这就是我需要弄清楚的东西。
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
知道了它,它似乎并没有为我做这件事(直到我意识到我并不特定于我想要的东西)所以基本上要更新,我要评估的字符串是(我知道它有点不正统,但要指明一点;我也希望这进一步澄清使其成功所需的东西):
(适用3D12 ^ D2-2)+ D4(2 * D4 / D2)
通过阅读本文,您可能会看到我不知道如何表现得非常好的景点......但这就是为什么我要求所有可爱的,聪明的程序员在那里!我希望我能够清楚地问这个问题并感谢你的时间:3
答案 0 :(得分:0)
任何编程问题的诀窍是分解它并为每个部分编写一个方法,所以下面我有一个滚动一个骰子的方法,由一个骰子调用一个骰子。
private Random rand = new Random();
/**
* @param roll can be a multipart roll which is run and added up. e.g. d6+2-d4
*/
public int multiPartRoll(String roll) {
String[] parts = roll.split("(?=[+-])"); //split by +-, keeping them
int total = 0;
for (String partOfRoll : parts) { //roll each dice specified
total += singleRoll(partOfRoll);
}
return total;
}
/**
* @param roll can be fixed value, examples -1, +2, 15 or a dice to roll
* d6, +d20 -d100
*/
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 = rand.nextInt(diceSize) + 1; //roll the dice
if (roll.startsWith("-")) //negate if nessasary
result = -result;
return result;
}