String [] A = {"High","Medium","Low"};
String [] B = {"High","Medium","Low"};
String [] C = {"High","Medium","Low"};
String [] D = {"High","Medium","Low"};
String [] E = {"High","Medium","Low"};
String [] F = {"High","Medium","Low"};

JComboBox Ai = new JComboBox(A); JComboBox Bi = new JComboBox(B);
JComboBox Ci = new JComboBox(C); JComboBox Di = new JComboBox(C);
JComboBox Ei = new JComboBox(E); JComboBox Fi = new JComboBox(F);

....

//add the user choice in arrayList
ArrayList<String> a = new ArrayList<String>();
a.add((String) Ai.getSelectedItem());
a.add((String) Bi.getSelectedItem());
a.add((String) Ci.getSelectedItem());
a.add((String) Di.getSelectedItem());
a.add((String) Ei.getSelectedItem());
a.add((String) Fi.getSelectedItem());

编辑:
场景:
有6组(Ai,Bi,Ci,Di,Ei,Fi)可供选择。每组有3个子选项(高(H),中(M),低(L))。用户需要在6个组中选择一个

选择可以是例如“HHHLLL”或“MMMLLM”或“HHLLMM”等。

在不编写许多else if的情况下检查和匹配用户选择的最佳方法是什么?
例如
if(Ai=="High" && Bi=="High" && Ci=="Low" && Di=="High" && Ei=="Low" && Fi=="Medium") {
    System.out.println("Good Choice");
}

谢谢。

最佳答案

首先,您不需要为每个JComboBox提供新的选择列表。

String[] choices = {"High", "Medium", "Low"};

JComboBox ai = new JComboBox(choices);
JComboBox bi = new JComboBox(choices);
JComboBox ci = new JComboBox(choices);
JComboBox di = new JComboBox(choices);
JComboBox ei = new JComboBox(choices);
JComboBox fi = new JComboBox(choices);

(Java中的变量通常以小写字母开头,因此我将变量名称更改为小写。)

接下来,您可以将所有六个JComboBox放入一个数组中。您稍后将看到为什么这很有用。
JComboBox[] boxes = {ai, bi, ci, di, ei, fi};

现在,您可以使用户选择字符串如下所示:
// Create an ArrayList of Strings, where each string is either "H", "M", or "L"
ArrayList<String> userChoice = new ArrayList<String>()

for (JComboBox box : boxes) {
    // Go through this code once for each JComboBox in boxes
    // The first time through, "box" means the first JComboBox
    // The second time through, "box" is the second JComboBox, etc.
    if (box.getValue().equals("High")) {
         userChoice.add("H");
    } else if (box.getValue().equals("Medium")) {
         userChoice.add("M");
    } else if (box.getValue().equals("Low")) {
         userChoice.add("L")
    }
}

您可能不熟悉for-each循环。它的意思是“对某物阵列中的每一个物进行一次此代码处理”。

如果使用此代码,则会得到一个名为userChoice的ArrayList,该数组具有类似[“H”,“H”,“M”,“M”,“L”,“L”]的名称。

10-06 13:19