检查是否所有 JComboBoxes 都选择了任何索引(索引 0 除外)
Check if all JComboBoxes have something selected any indexes (Other than index 0)
I have this code sample.
JComboBox[] set = {jComboBox1, jComboBox2, jComboBox3};
for (int i = 0; i < set.length; i++) {
JComboBox boxes = set[i];
int index = boxes.getSelectedIndex();
if (index == 0) {
System.out.println("Not every JcomboBox has something selected");
continue;
} else {
System.out.println("Every combobox has selected something");
}
我知道每次 for loop
运行时,它都会检查每个 JComboBox
的选定值。如果至少有一个 JComboBox
选择了某些东西(除 0 之外的所有其他索引),它将不会执行 if
语句,因此它会转到 else
语句。 但我需要打印 -Every combobox has selected something- 语句,当且仅当 all of JComboBoxes
have选择了一些东西(除索引 0 以外的任何索引)但没有他们中的任何一个选择了一些东西(即除 0 以外的任何索引)
谁能帮我解决这个问题???
尝试使用 flag
变量。
boolean allSelected = true; //Initially set it to true which indicates that all selected values are other than 0
for (int i = 0; i < set.length; i++) {
JComboBox boxes = set[i];
int index = boxes.getSelectedIndex();
if (index == 0) {
allSelected = false; //If index is 0, make it false
}
}
if(allSelected)
{
System.out.println("Every combobox has selected something");
}
else
{
System.out.println("Not every JcomboBox has something selected");
}
I have this code sample.
JComboBox[] set = {jComboBox1, jComboBox2, jComboBox3};
for (int i = 0; i < set.length; i++) {
JComboBox boxes = set[i];
int index = boxes.getSelectedIndex();
if (index == 0) {
System.out.println("Not every JcomboBox has something selected");
continue;
} else {
System.out.println("Every combobox has selected something");
}
我知道每次 for loop
运行时,它都会检查每个 JComboBox
的选定值。如果至少有一个 JComboBox
选择了某些东西(除 0 之外的所有其他索引),它将不会执行 if
语句,因此它会转到 else
语句。 但我需要打印 -Every combobox has selected something- 语句,当且仅当 all of JComboBoxes
have选择了一些东西(除索引 0 以外的任何索引)但没有他们中的任何一个选择了一些东西(即除 0 以外的任何索引)
谁能帮我解决这个问题???
尝试使用 flag
变量。
boolean allSelected = true; //Initially set it to true which indicates that all selected values are other than 0
for (int i = 0; i < set.length; i++) {
JComboBox boxes = set[i];
int index = boxes.getSelectedIndex();
if (index == 0) {
allSelected = false; //If index is 0, make it false
}
}
if(allSelected)
{
System.out.println("Every combobox has selected something");
}
else
{
System.out.println("Not every JcomboBox has something selected");
}