2016-12-17 112 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; 
     } 
    } 
}