【发布时间】:2014-03-03 17:44:27
【问题描述】:
我知道这很简单。但这就是困扰我的地方。有一个更好的方法吗? 数组通常并不重要,只是这段代码的目标是我要压缩的功能。
for(int p=0; p<28;p++){
if(p == 0){
Pile1[0] = deck[p];
}
if(p == 1 || p == 7){
if(p == 1){
Pile2[0] = deck[p];
}
if(p == 7){
Pile2[1] = deck[p];
}
}
if(p == 2 || p == 8 || p == 13){
if(p == 2){
Pile3[0] = deck[p];
}
if(p == 8){
Pile3[1] = deck[p];
}
if(p == 13){
Pile3[2] = deck[p];
}
}
if(p == 3 || p == 9 || p == 14 || p == 18){
if(p == 3){
Pile4[0] = deck[p];
}
if(p == 9){
Pile4[1] = deck[p];
}
if(p == 14){
Pile4[2] = deck[p];
}
if(p == 18){
Pile4[3] = deck[p];
}
}
if(p == 4 || p == 10 || p == 15 || p == 19 || p == 22){
if(p == 4){
Pile5[0] = deck[p];
}
if(p == 10){
Pile5[1] = deck[p];
}
if(p == 15){
Pile5[2] = deck[p];
}
if(p == 19){
Pile5[3] = deck[p];
}
if(p == 22){
Pile5[4] = deck[p];
}
}
if(p == 5 || p == 11 || p == 16 || p == 20 || p == 23 || p == 25){
if(p == 5){
Pile6[0] = deck[p];
}
if(p == 11){
Pile6[1] = deck[p];
}
if(p == 16){
Pile6[2] = deck[p];
}
if(p == 20){
Pile6[3] = deck[p];
}
if(p == 23){
Pile6[4] = deck[p];
}
if(p == 25){
Pile6[5] = deck[p];
}
}
if(p == 6 || p == 12 || p == 17 || p == 21 || p == 24 || p == 26 || p == 27){
if(p == 6){
Pile7[0] = deck[p];
}
if(p == 12){
Pile7[1] = deck[p];
}
if(p == 17){
Pile7[2] = deck[p];
}
if(p == 21){
Pile7[3] = deck[p];
}
if(p == 24){
Pile7[4] = deck[p];
}
if(p == 26){
Pile7[5] = deck[p];
}
if(p == 27){
Pile7[6] = deck[p];
}
}
}
【问题讨论】:
-
“这段代码的目标是什么” - 我自己也问不出更好的问题了……
-
你考虑过枚举类型吗?
-
什么是
PileX[其中 X >= 1 && X deck 是什么?至少显示整个代码。你也应该试试codereview.stackexchange.com。 -
首先,取出for循环。无需循环 28 次。
-
这是什么语言?请适当标记。当你在做的时候,写一个更具描述性的标题。