2016-12-17 108 views
0

我正在Android Studio中构建复杂的骰子滚轮。这需要用户输入并执行计算。我已经想出了如何使用.split函数来添加或减去字符串。但是,我无法根据公式中的哪个运算符来将它加到和减去。附加是仅添加的代码的图片。 Complex Roller Add下面是不工作的代码,我试图让它加入和减去。如果有任何可以帮助我,我将不胜感激。Android Studio中的复杂骰子滚轮应用程序

公共无效卷(){

String equationAdd = et_roll1.getText().toString(); 
String equationSubtract = et_roll1.getText().toString(); 
String[]rollsAdd = equationAdd.split("\\+"); 
String[]rollsSubtract = equationSubtract.split("-"); 

for (String dieTypeAdd:rollsAdd) 
{ 
    for (String dieTypeSubtract:rollsSubtract){ 
    String[] dieDataAdd = dieTypeAdd.split("d"); 
    String[] dieDataSubtract = dieTypeSubtract.split("d"); 
    if (dieDataAdd.length > 1) 
    { 
     int dieQty = Integer.parseInt(dieDataAdd[0]); 
     int dieFace = Integer.parseInt(dieDataAdd[1]); 
     for (int i = 0; i < dieQty; i++) 
     { 
      int roll = r.nextInt(dieFace); 
      sumAdd += roll; 
     } 
    } 
    else 
    { 
     int modifier = Integer.parseInt(dieDataAdd[0]); 
     sumAdd += modifier; 
    } 
    if (dieDataSubtract.length > 1) 
    { 
     int dieQty = Integer.parseInt(dieDataSubtract[0]); 
     int dieFace = Integer.parseInt(dieDataSubtract[0]); 
     for (int i = 0; i < dieQty; i++) 
     { 
      int roll = r.nextInt(dieFace); 
      sumSubtract -= roll; 
     } 
    } 
    else 
    { 
     int modifier = Integer.parseInt(dieDataSubtract[0]); 
     sumSubtract -= modifier; 
    } 
}} 

}

+0

还应当指出的是,我构建这个项目是一个“试验台”项目,以确保在实施我的实际项目之前能够正常工作。所以命名约定没有被锁定。 –

回答

0

想通了的人可能会寻找在未来的答案:

static void rolls(){ 
    //tells the progrom what characters to split the string by, "A" allows for either "+" or "-" at the begininning of the string 
    Pattern expressionFormat = Pattern.compile("((\\A|\\+|\\-){1}(\\d+[d])?\\d+)"); 

    String equationAdd = et_roll1.getText().toString(); 

    Matcher matcher = expressionFormat.matcher(equationAdd); 

    String rollsAdd = ""; 

    while(matcher.find()){ 
     rollsAdd = matcher.group(); 
     String[] diedataArray = rollsAdd.split("(\\+|\\-)"); //tells the program to isolate values with either "+" or "-" 
     String dieData = diedataArray[0]; 
     if (diedataArray[0].length() == 0) { 
      dieData = diedataArray[1]; 
     } 
     //splits the users input data further by looking for a value of "d", the number to the left of the "d" = times to roll, the number to the right of the "d" = number of faces on the die 
     if (rollsAdd.length() > 2){ 
      dieDataAdd = dieData.split("d"); 
      int dieQty = Integer.parseInt(dieDataAdd[0]); 
      int dieFace = Integer.parseInt(dieDataAdd[1]); 
      int rollSum = 0; 

      //rolls a dice based on the users input....ie. 1d6 rolls a single 6 sides dice, 3d20 rolls 3 different 20 sides dice 
      for (int i = 0; i < dieQty; i++){ 
       roll = r.nextInt(dieFace)+1; 
       rollSum += roll; 
      } 
      //program speicially looks for the "-" operator and then adds the value to the sumSubtract variable 
      if (rollsAdd.charAt(0) == '-'){ 
       sumSubtract += rollSum; 
      } 
      // if the operator is "+", add its value to sumAdd variable 
      else{ 
       sumAdd += rollSum; 
      } 
     } 
     //this accounts for any whole numbers that are not dice.....ie. 1d6+2, 2 being the modifier of the roll 
     else{ 
      int modifier = Integer.parseInt(rollsAdd); 
      modTotal += modifier; 
     } 
     //these 2 IF statements test to see which value is the highest, then subtracts (only works if user inputs subtraction) the highest first in order to "always" show a positive number 
     if (sumAdd > sumSubtract){ 
      sum = sumAdd - sumSubtract + modTotal; 
     } 
     else if (sumSubtract > sumAdd){ 
      sum = sumSubtract - sumAdd + modTotal; 
     } 
     else{ 
      sum = 0; 
     } 
    } 
}