2012-04-20 100 views
-1

我是编程新手,但是我需要按元素进行分组,如果它们是由用户选择的;因此,我不确定我的小组将包含多少元素。 因此,我这样做是为了计算选定元素的数量,然后创建另一个语句,创建正确的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"; 
} 
+2

使用'switch'语句而不是所有那些糟糕的分支。 – 2012-04-20 16:39:45

+1

虽然这个问题可能属于“代码审查”。 – 2012-04-20 16:50:54

回答

4

循环!这里有一个实施建议:

// adding all the vars to an array might improve readability 
boolean checks[] = new boolean[] { endbox, coursebox, departmentbox, callbox, 
            daybox, startbox, instructorbox, roombox, 
            buildingbox, numberenrolled }; 

for (boolean check : checks) { 
    if (check) { 
     totalOutPuts++; 
    } 
} 

String groupBy = " Group by "; 

// this isn't the best way of appending strings in Java, 
// you'd be better using StringBuilder. this is for the sake of simplicity 
for (int i = 1; i < totalOutPuts; i++) { 
    groupBy += i + ", "; 
} 

// append the last number 
groupBy += totalOutPuts + ""; 
3

脱颖而出第一件事:

totalOutPuts = endbox == true ? totalOutPuts + 1 : totalOutPuts; 

比较一个布尔值,true是没有意义的,因为它已经是一个布尔值,只是这样做:

totalOutPuts = endbox ? totalOutPuts + 1 : totalOutPuts; 

除此之外,你可以进一步简化为:

totalOutPuts += endbox ? 1 : 0; // same as totalOutPuts = totalOutPuts + (endbox ? 1 : 0); 

或者,更可读:

if(endbox) { 
    totalOutPuts += 1; // or ++totalOutPuts if you're into that sort of thing 
} 

然后在底部,你这样做:

int missingElements = 10 - totalOutPuts; 

那为什么不这样做摆在首位?

int missingElements = 10; 
if(endbox) { 
    missingElements -= 1; // this means missingElements = missingElements - 1; 
} 
// etc