-1

私はプログラミングに不慣れですが、要素がユーザーによって選択されている場合は、要素ごとにグループ化する必要があります。したがって、groupbyに含まれる要素の数が正確にはわかりません。したがって、選択した要素の量をカウントするためにこれを行い、次に、必要な要素の量を表す正しいGroupbyステートメントを作成する別のステートメントを作成しました。

それは動作しますが、かさばるように見えます。これを行うためのより良い方法があるかどうかを確認したいと思いますか?

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

最初に目立つのは:

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
于 2012-04-20T16:38:56.850 に答える