-1

I am new to programming, but I have a need to group by elements, if they are selected by user; therefore, I am not exactly sure how many elements my group by will contain. Therefore I did this to count amount of selected elements and then, created another statement that creates correct Group by statement that represents the amount of elements needed.

It works, but it looks bulky and I would like to see if there are better ways of doing this?

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";
}
4

2 回答 2

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 + "";
于 2012-04-20T16:47:19.677 回答
3

First thing that stands out:

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

Comparing a boolean to true is pointless, since it's already a boolean, just do this:

totalOutPuts = endbox ? totalOutPuts + 1 : totalOutPuts;

Beyond that, you could simplify further to:

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

Or, more readable:

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

Then at the bottom, you do this:

int missingElements = 10 - totalOutPuts;

So why not just do that in the first place?

int missingElements = 10;
if(endbox) {
    missingElements -= 1; // this means missingElements = missingElements - 1;
}
// etc
于 2012-04-20T16:38:56.850 回答