0

我注意到类似的重复并尝试使用单个 for 循环来解决此问题,如果我可以最小化代码长度:

如果我可以形成一个循环,我就不需要使用开关盒了吗?

  1. $returnNo 变量从 5 开始,每个 case 乘以 2 然后减 1。

  2. 它显示“$a<=”,它从 5 开始,每个 case 乘以 2,然后再加 3。

  3. if() 语句从 if($matchno == 7) 开始,每个 case 乘以 2 然后加 1。

  4. 最后的 if() 语句从 if($matchno == 8) 开始,每个 case 乘以 2。

  5. 我已经完成了案例 64,它实际上会上升到 512。我知道代码在重复,我希望有人可以帮助我为此生成一个循环?

非常感谢!

    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;      

    }
}
4

2 回答 2

1

我将以前两种情况为例:

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

简单解释一下,删除一直重复的代码并尝试对其进行参数化(通过创建函数或将所有重复的代码放在其他地方......在这个示例中,我将重复代码放在 switch 语句之后并对其进行参数化。

于 2013-08-20T20:14:30.310 回答
0

如果我理解得很好,这是一个替代解决方案,我认为它适用于您指定的所有情况,不使用 switch 案例。

$div10 = $max / 10;
$maxLoop = $div10 - 3;
$returnNO = $div10 / 2 + 1;

for($a = 1; $a<=$maxLoop; $a++) {
    if($matchno == $a || $matchno == ($a+1)){
        $matchinfo['matchno'] = $returnNO;
        $matchinfo['place'] = ($matchno == $a ? 'clan1' : 'clan2');
        return $matchinfo;
    }
    $returnNO++;
    $a++;
}
if($matchno == ($div10-1)){
    $matchinfo['winner'] = true;
    return $matchinfo;
}elseif($matchno == $div10){
    $matchinfo['third_winner'] = true;
    return $matchinfo;
}
于 2013-08-20T20:16:29.407 回答