Android:将一组函数转换为等式

时间:2014-03-06 11:20:56

标签: android equation

我99%确定无法做到这一点,但我想我会要求确定。

我正在尝试创建一个应用程序,为流行的桌面战争游戏中的动作计算所需的骰子掷骰。

以下是Java中的计算

        int x = ((WSattacker * 2) - WSdefender);
        int y = (WSattacker - WSdefender);

        String result;

        // Calculation for a +5
        if (x <= -1) {

            result = "5+";
        }

        // Calculation for a +4
        else if (x >= 0 && y <= 0) {

            result = "4+";
        }

        // Calculation for a +3
        else if (y > 0) {

            result = "3+";
        } else {

            result = "Error";
        }

        return result;

现在我的问题是,为了避免广告文案侵权,我不能在我的应用程序中提及游戏的名称,并且可能无法在应用程序中对上述计算进行硬编码。

这意味着很难告诉潜在用户该应用会做什么。

我能想到的唯一解决方案是使应用程序通用,并允许用户以等式的形式输入所需的计算。

我可以匿名放在公共董事会或类似公告上的等式。

因此我的问题如下。

  1. 还有另一种解决方法吗?
  2. 如果不是,是否可以将上述代码压缩成单个表达式/方程式。一个删除if和else语句的文件

1 个答案:

答案 0 :(得分:1)

回答问题2:

result = test_condition_1 ? result2_if_true : (test_condition_2 ? result2_if_true : test3_or_result2);

然后,您可以通过这种方式构建“复合”测试条件,并且它基于ternary operators

修改

三元运算符是编写if..then..else语句的简便方法,更多信息可以在上面的wiki-links中找到。下面是一个使用它的例子,您可以编译并运行它:

public class TernaryTest {

    public static void main(String [] args){
    int x = 14;
    int y = 5;
        String result = ( x <= 10 ) ? "Less than 10" : "More than 10";
        System.out.println("Result is: " + result);
    }
}

尝试运行它并在更改x的值时查看结果以了解其工作原理。然后,可以通过替换else字符串将其扩展为包含和"more than 10"