我是编程新手,但是我需要按元素进行分组,如果它们是由用户选择的;因此,我不确定我的小组将包含多少元素。 因此,我这样做是为了计算选定元素的数量,然后创建另一个语句,创建正确的Group by语句,该语句表示所需元素的数量。我正确吗?
它的工作原理,但它看起来很笨重,我想看看是否有更好的方法来做到这一点?
int totalOutPuts = 0;
totalOutPuts = endbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = coursebox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = departmentbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = callbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = daybox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = startbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = instructorbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = roombox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = buildingbox == true ? totalOutPuts + 1 : totalOutPuts;
totalOutPuts = numberenrolled == true ? totalOutPuts + 1 : totalOutPuts;
int missingElements = 10 - totalOutPuts;
String groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
if (missingElements == 9) {
groupBy = " Group by 1";
} else if (missingElements == 8) {
groupBy = " Group by 1, 2";
} else if (missingElements == 7) {
groupBy = " Group by 1, 2, 3";
} else if (missingElements == 6) {
groupBy = " Group by 1, 2, 3, 4";
} else if (missingElements == 5) {
groupBy = " Group by 1, 2, 3, 4, 5";
} else if (missingElements == 4) {
groupBy = " Group by 1, 2, 3, 4, 5, 6";
} else if (missingElements == 3) {
groupBy = " Group by 1, 2, 3, 4, 5, 6, 7";
} else if (missingElements == 2) {
groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8";
} else if (missingElements == 1) {
groupBy = " Group by 1, 2, 3, 4, 5, 6, 7, 8, 9";
}
使用'switch'语句而不是所有那些糟糕的分支。 – 2012-04-20 16:39:45
虽然这个问题可能属于“代码审查”。 – 2012-04-20 16:50:54