J'ai été à cette toute la journée et je ne peux pas pour la vie de moi comprendre ce que je fais mal. Le code plante mon émulateur Android. Ce que j'essaye de faire est d'écrire un programme de dés, et je l'ai en train de travailler avec des dés lancés comme des entités uniques, mais j'essaie de traiter des jets de dés comme 3d6 ou 5d4. L'appel à singleRoll lance un seul dé à la fois, et j'essaie de décomposer une équation plus longue en bits plus simples ... "3d4 + 5-13d6 + 7d8 + 9"Aide à l'expression régulière dans le programme de dés Java
bbs.randInt renvoie [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;
}
Vous recevez une erreur? Si c'est le cas, postez la trace de la pile. – shmosel
où est la méthode 'singleRoll (String)'? Pourriez-vous poster ce code aussi? – Alexander
'public int singleRoll (Rouleau de chaînes) { int di = roll.indexOf ('d'); if (di == -1) // cas où il n'y a pas 'd' return Integer.parseInt (roll); int diceSize = Integer.parseInt (rouleau.sous-chaîne (di + 1)); // valeur de la chaîne après 'd' int résultat = bbs.randInt (diceSize) + 1; // lancer les dés if (roll.startsWith ("-")) // annuler si nessasaire result = -result; renvoie le résultat; } ' –