Android Studio中的复杂骰子滚轮应用程序

时间:2016-12-17 04:56:26

标签: android

我正在Android Studio中构建复杂的骰子滚轮。这需要用户输入并预先计算。我已经想出如何使用.split函数添加或减去字符串。但是,根据哪个运算符在等式中,我无法将其添加到AND减去。附件是只添加的代码图片。 http://tomerlerner.com/下面是无效的代码,我试图将其添加到减去。如果有任何可以帮助我,我会很感激。

public void rolls(){

div.site-top {
 z-index:999;
}

div.container {
 position:relative;
 z-index:9;
}

}

1 个答案:

答案 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;
        }
    }
}