Android如何创建选项矩阵?

时间:2016-06-21 16:24:45

标签: android

我正在创建一个Android原生应用程序,用户可以在其中获得他们想要做的一种锻炼类型的多种选择。他们可以选择强度,持续时间和锻炼量。每种特定的选择组合都有特定的结果。例如:

choice 1:
a b c
choice 2:
1 2 3
choice 3:
. , /

在这种情况下,有9种可能的结果。如果用户选择3个选项,则会获得特定结果。

a 1 . = outcome 1
a 1 , = outcome 2
a 1 / = outcome 3
a 2 . = outcome 4
etc.

我想知道最好的方法是什么?我可以创建一个精心设计的if / else函数,但这感觉就像解决这个问题一样糟糕。这感觉可能是一个愚蠢的问题,但我现在无法想出任何东西。

1 个答案:

答案 0 :(得分:1)

在这里,我提出了一个相当简单的问题解决方案。由于我不知道你打算如何在android中实现它,我已经用Java提供了一个解决方案。

您需要将选择存储为所有选项的数组。

public static final int combination1_length = 2;
public static final int combination2_length = 2;
public static final int combination3_length = 2;

String[] combination1 = new String[combination1_length];
String[] combination2 = new String[combination2_length];
String[] combination3 = new String[combination3_length];

System.out.println("Enter the value of the combinations : ");

System.out.println("Enter values for first array :");
for (int i=0; i<combination1.length; i++) combination1[i] = in.nextLine();
System.out.println("Enter values for second array :");
for (int i=0; i<combination2.length; i++) combination2[i] = in.nextLine();
System.out.println("Enter values for third array :");
for (int i=0; i<combination3.length; i++) combination3[i] = in.nextLine();

然后你需要创建一个结果矩阵,其维度为array_choices1.length * array_choices2.length * ..... * array_choicesN.length。

String[][][] outcomes = new String[combination1_length]
    [combination2_length][combination3_length];

System.out.println("Enter outcomes of the combinations :");
    for (int i=0; i<outcomes.length; i++)
        for (int j=0; j<outcomes[0].length; j++)
            for (int k=0; k<outcomes[0][0].length; k++)
                outcomes[i][j][k] = in.nextLine();

然后,您需要从用户那里获取输入组合。

System.out.println("Enter the combinations preferred : ");
String input1 = in.nextLine();
String input2 = in.nextLine();
String input3 = in.nextLine();

从相应的数组中进行选择性匹配,并存储每个选择数组的匹配索引。

int index1 = 0, index2 = 0, index3 = 0;
for (index1 = 0; index1<combination1.length; index1++)
    if (combination1[index1].equals(input1)) break;
for (index2 = 0; index2<combination2.length; index2++)
    if (combination2[index2].equals(input2)) break;
for (index3 = 0; index3<combination3.length; index3++)
    if (combination3[index3].equals(input3)) break;

结果如下:

System.out.println("The outcome is : " + outcomes[index1][index2][index3]);

完整的代码是:

public static final int combination1_length = 2;
public static final int combination2_length = 2;
public static final int combination3_length = 2;

public static void main(String[] args){
    Scanner in = new Scanner(System.in);

    String[] combination1 = new String[combination1_length];
    String[] combination2 = new String[combination2_length];
    String[] combination3 = new String[combination3_length];

    String[][][] outcomes = new String[combination1_length]
    [combination2_length][combination3_length];

    System.out.println("Enter the value of the combinations : ");

    System.out.println("Enter values for first array :");
    for (int i=0; i<combination1.length; i++) combination1[i] = in.nextLine();
    System.out.println("Enter values for second array :");
    for (int i=0; i<combination2.length; i++) combination2[i] = in.nextLine();
    System.out.println("Enter values for third array :");
    for (int i=0; i<combination3.length; i++) combination3[i] = in.nextLine();

    System.out.println("Enter outcomes of the combinations :");
    for (int i=0; i<outcomes.length; i++)
        for (int j=0; j<outcomes[0].length; j++)
            for (int k=0; k<outcomes[0][0].length; k++)
                outcomes[i][j][k] = in.nextLine();

    System.out.println("Enter the combinations preferred : ");
    String input1 = in.nextLine();
    String input2 = in.nextLine();
    String input3 = in.nextLine();

    int index1 = 0, index2 = 0, index3 = 0;
    for (index1 = 0; index1<combination1.length; index1++)
        if (combination1[index1].equals(input1)) break;
    for (index2 = 0; index2<combination2.length; index2++)
        if (combination2[index2].equals(input2)) break;
    for (index3 = 0; index3<combination3.length; index3++)
        if (combination3[index3].equals(input3)) break;
    // System.out.println("Index1 : " + index1 + ", Index2 : " + index2 + ", Index3 : " + index3);
    System.out.println("The outcome is : " + outcomes[index1][index2][index3]);

我没有处理边界案件。假设用户输入的选项不在您选择的数组中。在这种情况下,应显示相应的错误消息。但我想你会处理这个,因为你会在android中spinners的行上实现一些东西,从而消除了非法输入的可能性。