dsxay48646 2013-08-20 19:57
浏览 15

解决循环[关闭]

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?

  1. $returnNo variable starts at 5, each case multiplied by 2 then minus 1.

  2. where it shows "$a<=", it starts at 5 and each case multiplied by 2 then plus 3.

  3. the if() statement starting at if($matchno == 7), each case multiplied by 2 then plus 1.

  4. the final if() statement starting at if($matchno == 8), each case multiplied by 2.

  5. 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?

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;      

    }
}
  • 写回答

2条回答 默认 最新

  • dongtui4038 2013-08-20 20:14
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测