解决循环 [英] Working around for loop
问题描述
我已经注意到类似的重复,并试图解决这个问题,如果我可以尽量减少代码长度:
我不需要使用开关的情况下,如果我可以形成一个循环,而不是?
-
$ returnNo变量从5开始,每个case乘以2 then
-
它显示$ a< =,它从5开始,每个大小写乘以2再加上3。
/ li> -
从if($ matchno == 7)开始的if()语句,每个case乘以2再加上1。
- 到64的情况下,实际上会达到512.据我所知,代码重复我希望有人可以帮助我产生一个单一的循环吗?
$returnNo variable starts at 5, each case multiplied by 2 then minus 1.
where it shows "$a<=", it starts at 5 and each case multiplied by 2 then plus 3.
the if() statement starting at if($matchno == 7), each case multiplied by 2 then plus 1.
the final if() statement starting at if($matchno == 8), each case multiplied by 2.
I have done up to case 64, it will actually go up to 512. As I know the code is repeating I am hoping someone can help me produce a single loop for this?
非常感谢!
开关($ max){
案例80:
$ returnNO = 5; ($ a = 1; $ a< = 5; $ a ++){
if($ matchno == $ a || $ matchno ==($ a + 1)){$ b $
b $ matchinfo ['matchno'] = $ returnNO;
$ matchinfo ['place'] =($ matchno == $ a?'clan1':'clan2');
返回$ matchinfo;
}
$ returnNO ++;
$ a ++;
if($ matchno == 7){
$ matchinfo ['winner'] = true;
返回$ matchinfo;
} elseif($ matchno == 8){
$ matchinfo ['third_winner'] = true;
返回$ matchinfo;
}
break;
案例160:
$ returnNO = 9; ($ a = 1; $ a< = 13; $ a ++){
if($ matchno == $ a || $ matchno ==($ a + 1)){
$ matchinfo ['matchno'] = $ returnNO;
$ matchinfo ['place'] =($ matchno == $ a?'clan1':'clan2');
返回$ matchinfo;
}
$ returnNO ++;
$ a ++;
if($ matchno == 15){
$ matchinfo ['winner'] = true;
返回$ matchinfo;
} elseif($ matchno == 16){
$ matchinfo ['third_winner'] = true;
返回$ matchinfo;
}
break;
案例320:
$ returnNO = 17; ($ a = 1; $ a< = 29; $ a ++){
if($ matchno == $ a || $ matchno ==($ a + 1)){
$ matchinfo ['matchno'] = $ returnNO;
$ matchinfo ['place'] =($ matchno == $ a?'clan1':'clan2');
返回$ matchinfo;
}
$ returnNO ++;
$ a ++;
}
if($ matchno == 31){
$ matchinfo ['winner'] = true;
返回$ matchinfo;
} elseif($ matchno == 32){
$ matchinfo ['third_winner'] = true;
返回$ matchinfo;
}
break;
case 640:
$ returnNO = 33; ($ a = 1; $ a< = 61; $ a ++){
if($ matchno == $ a || $ matchno ==($ a + 1)){$ b $
b $ matchinfo ['matchno'] = $ returnNO;
$ matchinfo ['place'] =($ matchno == $ a?'clan1':'clan2');
返回$ matchinfo;
}
$ returnNO ++;
$ a ++;
if($ matchno == 63){
$ matchinfo ['winner'] = true;
返回$ matchinfo;
} elseif($ matchno == 64){
$ matchinfo ['third_winner'] = true;
返回$ matchinfo;
}
break;
$ b $ / code $ / $ p
解决方案
switch($ max){
case 80:
$ returnNO = 5;
$ loopCount = 5;
$赢家= 7;
$ thirdWinner = 8;
break;
案例160:
$ returnNO = 9;
$ loopCount = 13;
$赢家= 15;
$ thirdWinner = 16;
break;
$ b $($ a = 1; $ a< = $ loopCount; $ a ++){
if($ matchno == $ a || $ matchno ==($ a + 1)){
$ matchinfo ['matchno'] = $ returnNO;
$ matchinfo ['place'] =($ matchno == $ a?'clan1':'clan2');
返回$ matchinfo;
$ b $ if($ matchno == $ winner){
$ matchinfo ['winner'] = true;
返回$ matchinfo;
} else if($ matchno == $ thirdWinner){
$ matchinfo ['third_winner'] = true;
返回$ matchinfo;
$ b简单地解释一下,删除一直重复的代码并尝试对它进行参数化(通过创建一个函数或者把所有重复的代码放在其他地方...在这个例子中,我把重复的代码放在switch语句之后,并且将其重新计算。
I have noticed similar repetition and trying to work around using a single for loop for this if I can to minimize the code length:
I wouldn't need to use a switch case if I can form a loop instead?
Many thanks!
switch($max) {
case 80 :
$returnNO = 5;
for($a = 1; $a<=5; $a++) {
if($matchno == $a || $matchno == ($a+1)){
$matchinfo['matchno'] = $returnNO;
$matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
return $matchinfo;
}
$returnNO++;
$a++;
}
if($matchno == 7){
$matchinfo['winner'] = true;
return $matchinfo;
}elseif($matchno == 8){
$matchinfo['third_winner'] = true;
return $matchinfo;
}
break;
case 160 :
$returnNO = 9;
for($a = 1; $a<=13; $a++) {
if($matchno == $a || $matchno == ($a+1)){
$matchinfo['matchno'] = $returnNO;
$matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
return $matchinfo;
}
$returnNO++;
$a++;
}
if($matchno == 15){
$matchinfo['winner'] = true;
return $matchinfo;
}elseif($matchno == 16){
$matchinfo['third_winner'] = true;
return $matchinfo;
}
break;
case 320 :
$returnNO = 17;
for($a = 1; $a<=29; $a++) {
if($matchno == $a || $matchno == ($a+1)){
$matchinfo['matchno'] = $returnNO;
$matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
return $matchinfo;
}
$returnNO++;
$a++;
}
if($matchno == 31){
$matchinfo['winner'] = true;
return $matchinfo;
} elseif($matchno == 32){
$matchinfo['third_winner'] = true;
return $matchinfo;
}
break;
case 640 :
$returnNO = 33;
for($a = 1; $a<=61; $a++) {
if($matchno == $a || $matchno == ($a+1)){
$matchinfo['matchno'] = $returnNO;
$matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
return $matchinfo;
}
$returnNO++;
$a++;
}
if($matchno == 63){
$matchinfo['winner'] = true;
return $matchinfo;
}elseif($matchno == 64){
$matchinfo['third_winner'] = true;
return $matchinfo;
}
break;
}
}
解决方案 I'll use the first two cases as an example:
switch ($max) {
case 80:
$returnNO = 5;
$loopCount = 5;
$winner = 7;
$thirdWinner = 8;
break;
case 160:
$returnNO = 9;
$loopCount = 13;
$winner = 15;
$thirdWinner = 16;
break;
...
}
for ($a = 1; $a <= $loopCount; $a++) {
if ($matchno == $a || $matchno == ($a + 1)) {
$matchinfo['matchno'] = $returnNO;
$matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
return $matchinfo;
}
}
if ($matchno == $winner) {
$matchinfo['winner'] = true;
return $matchinfo;
} else if ($matchno == $thirdWinner) {
$matchinfo['third_winner'] = true;
return $matchinfo;
}
Simply explained, remove the code that repeats all the time and try to parameterize it (either by creating a function or by putting all repeated code somewhere else... in this example, I put the repeating code after the switch statement and paremeterized it.
这篇关于解决循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!