简单的数学解决方案不起作用

时间:2018-07-07 14:06:18

标签: php math

我想互相比较结果,因此如果$ ship <$ given = destroy =显示echo。它仅适用于sec90,其他人则一次忘记了后敌人>武器部分,因此即使敌人之前被摧毁,它也只显示飞船> trader46。

这里是一个例子:

if($ship > $_POST[trader46])
{
$taken = $_POST[trader46] * 2;

if($ship > $taken)
{
$taken3 = $_POST[trader46] * 3;
if($ship > $taken3)
{
$taken4 = $_POST[trader46] * 4;
if($ship > $taken4)
{
$taken5 = $_POST[trader46] * 5;
if($ship > $taken5)
{
$taken6 = $_POST[trader46] * 6;
if($ship > $taken6)
{
$taken7 = $_POST[trader46] * 7;
if($ship > $taken7)
{
$taken8 = $_POST[trader46] * 8;
if($ship > $taken8)
{
$taken9 = $_POST[trader46] * 9;
if($ship > $taken9)
{
$taken10 = $_POST[trader46] * 10;
if($ship > $taken10)
{
$taken11 = $_POST[trader46] * 11;
if($ship > $taken11)
{
$taken12 = $_POST[trader46] * 12;
if($ship > $taken12)
{
$taken13 = $_POST[trader46] * 13;
if($ship > $taken13)
{
$taken14 = $_POST[trader46] * 14;
if($ship > $taken14)
{
$taken15 = $_POST[trader46] * 15;
if($ship > $taken15)
{


if($_POST[enemy] > $weapon2)
{
$given = $weapon2 * 2;
if($_POST[enemy] > $given)
{
$given3 = $weapon2 * 3;
if($_POST[enemy] > $given3)
{
$given4 = $weapon2 * 4;
if($_POST[enemy] > $given4)
{
$given5 = $weapon2 * 5;
if($_POST[enemy] > $given5)
{
$given6 = $weapon2 * 6;
if($_POST[enemy] > $given6)
{
$given7 = $weapon2 * 7;
if($_POST[enemy] > $given7)
{
$given8 = $weapon2 * 8;
if($_POST[enemy] > $given8)
{
$given9 = $weapon2 * 9;
if($_POST[enemy] > $given9)
{
$given10 = $weapon2 * 10;
if($_POST[enemy] > $given10)
{
$given11 = $weapon2 * 11;
if($_POST[enemy] > $given11)
{
$given12 = $weapon2 * 12;
if($_POST[enemy] > $given12)
{
$given13 = $weapon2 * 13;
if($_POST[enemy] > $given13)
{
$given14 = $weapon2 * 14;
if($_POST[enemy] > $given14)
{
$given15 = $weapon2 * 15;
if($_POST[enemy] > $given15)
{
echo "Enemy still stands at minute fifteen";
}
else
{
echo "U destroyed the enemy in the fifteenth minute";
}
}
else
{
echo "U destroyed the enemy in the fourteenth minute";
}
}
else
{
echo "U destroyed the enemy in the thirteenth minute";
}
}
else
{
echo "U destroyed the enemy in the twelvth minute";
}
}
else
{
echo "U destroyed the enemy in the eleventh minute";
}
}
else
{
echo "U destroyed the enemy in the tenth minute";
}
}
else
{
echo "U destroyed the enemy in the ninth minute";
}
}
else
{
echo "U destroyed the enemy in the eight minute";
}
}
else
{
echo "U destroyed the enemy in the seventh minute";
}
}
else
{
echo "U destroyed the enemy in the sixt minute";
}
}
else
{
echo "U destroyed the enemy in the fifth minute";
}
}
else
{
echo "U destroyed the enemy in the fourth minute";
}
}
else
{
echo "U destroyed the enemy in the third minute";
}
}
else
{
echo "u destroyed the enemy in the second minute";
}
}
else
{
echo "enemy destroyed in the first minute";
}

}
else
{
echo "u got destroyed in the fifteenth minute";
}

}
else
{
echo "u got destroyed in the fourteenth minute";
}

}
else
{
echo "u got destroyed in the thirteenth minute";
}

}
else
{
echo "u got destroyed in the twelvth minute";
}

}
else
{
echo "u got destroyed in the eleventh minute";
}

}
else
{
echo "u got destroyed in the tenth minute";
}

}
else
{
echo "u got destroyed in the ninth minute";
}

}
else
{
echo "u got destroyed in the eight minute";
}

}
else
{
echo "u got destroyed in the seventh minute";
}

}
else
{
echo "u got destroyed in the sixt minute";
}

}
else
{
echo "u got destroyed in the fifth minute";
}

}
else
{
echo "u got destroyed in the fourth minute";
}

}
else
{
echo "u got destroyed in the third minute";
}

}
else
{
echo "u got destroyed in the second minute";
}

}
else
{
echo "u got destroyed in the first minute";
}
}

2 个答案:

答案 0 :(得分:1)

我知道您说过别无选择。希望您不要介意我还是这样做了。如果您以如下方式构建代码,您将使未来变得更加幸福:

我假设您具有以下变量:$shipLife是玩家的生命值,$shipDamage是刨床造成的伤害,$enemyLife是敌人的生命值$enemyDamage是敌人造成的伤害。

//advance time $t with a for loop at most until minute 15
for($t = 1; $t <= 15; ++$t) 
{
    //perform enemy attack
    $shipLife -= $enemyDamage;

    //check if the ship survived the attack
    if($shipLife <= 0)
    {
        echo "You got destroyed in minute " . $t;
        break;
    }

    //perform ship attack
    $enemyLife -= $shipDamage;

    //check if the enemy survived the attack
    if($enemyLife <= 0)
    {
        echo "You destroyed the enemy in minute " . $t;
        break;
    }
}

//Check if both players are still alive
if($shipLife > 0 && $enemyLife > 0)
    echo "Both ship and enemy are still alive after " . ($t - 1) . " minutes";

答案 1 :(得分:1)

我用http://phpbeautifier.com/beautify.php重新格式化了您的在线代码

<?php

if ($ship > $_POST[trader46]) {
  $taken = $_POST[trader46] * 2;
  if ($ship > $taken) {
    $taken3 = $_POST[trader46] * 3;
    if ($ship > $taken3) {
      $taken4 = $_POST[trader46] * 4;
      if ($ship > $taken4) {
        $taken5 = $_POST[trader46] * 5;
        if ($ship > $taken5) {
          $taken6 = $_POST[trader46] * 6;
          if ($ship > $taken6) {
            $taken7 = $_POST[trader46] * 7;
            if ($ship > $taken7) {
              $taken8 = $_POST[trader46] * 8;
              if ($ship > $taken8) {
                $taken9 = $_POST[trader46] * 9;
                if ($ship > $taken9) {
                  $taken10 = $_POST[trader46] * 10;
                  if ($ship > $taken10) {
                    $taken11 = $_POST[trader46] * 11;
                    if ($ship > $taken11) {
                      $taken12 = $_POST[trader46] * 12;
                      if ($ship > $taken12) {
                        $taken13 = $_POST[trader46] * 13;
                        if ($ship > $taken13) {
                          $taken14 = $_POST[trader46] * 14;
                          if ($ship > $taken14) {
                            $taken15 = $_POST[trader46] * 15;
                            if ($ship > $taken15) {
                              if ($_POST[enemy] > $weapon2) {
                                $given = $weapon2 * 2;
                                if ($_POST[enemy] > $given) {
                                  $given3 = $weapon2 * 3;
                                  if ($_POST[enemy] > $given3) {
                                    $given4 = $weapon2 * 4;
                                    if ($_POST[enemy] > $given4) {
                                      $given5 = $weapon2 * 5;
                                      if ($_POST[enemy] > $given5) {
                                        $given6 = $weapon2 * 6;
                                        if ($_POST[enemy] > $given6) {
                                          $given7 = $weapon2 * 7;
                                          if ($_POST[enemy] > $given7) {
                                            $given8 = $weapon2 * 8;
                                            if ($_POST[enemy] > $given8) {
                                              $given9 = $weapon2 * 9;
                                              if ($_POST[enemy] > $given9) {
                                                $given10 = $weapon2 * 10;
                                                if ($_POST[enemy] > $given10) {
                                                  $given11 = $weapon2 * 11;
                                                  if ($_POST[enemy] > $given11) {
                                                    $given12 = $weapon2 * 12;
                                                    if ($_POST[enemy] > $given12) {
                                                      $given13 = $weapon2 * 13;
                                                      if ($_POST[enemy] > $given13) {
                                                        $given14 = $weapon2 * 14;
                                                        if ($_POST[enemy] > $given14) {
                                                          $given15 = $weapon2 * 15;
                                                          if ($_POST[enemy] > $given15) {
                                                            echo "Enemy still stands at minute fifteen";
                                                          }
                                                          else {
                                                            echo "U destroyed the enemy in the fifteenth minute";
                                                          }
                                                        }
                                                        else {
                                                          echo "U destroyed the enemy in the fourteenth minute";
                                                        }
                                                      }
                                                      else {
                                                        echo "U destroyed the enemy in the thirteenth minute";
                                                      }
                                                    }
                                                    else {
                                                      echo "U destroyed the enemy in the twelvth minute";
                                                    }
                                                  }
                                                  else {
                                                    echo "U destroyed the enemy in the eleventh minute";
                                                  }
                                                }
                                                else {
                                                  echo "U destroyed the enemy in the tenth minute";
                                                }
                                              }
                                              else {
                                                echo "U destroyed the enemy in the ninth minute";
                                              }
                                            }
                                            else {
                                              echo "U destroyed the enemy in the eight minute";
                                            }
                                          }
                                          else {
                                            echo "U destroyed the enemy in the seventh minute";
                                          }
                                        }
                                        else {
                                          echo "U destroyed the enemy in the sixt minute";
                                        }
                                      }
                                      else {
                                        echo "U destroyed the enemy in the fifth minute";
                                      }
                                    }
                                    else {
                                      echo "U destroyed the enemy in the fourth minute";
                                    }
                                  }
                                  else {
                                    echo "U destroyed the enemy in the third minute";
                                  }
                                }
                                else {
                                  echo "u destroyed the enemy in the second minute";
                                }
                              }
                              else {
                                echo "enemy destroyed in the first minute";
                              }
                            }
                            else {
                              echo "u got destroyed in the fifteenth minute";
                            }
                          }
                          else {
                            echo "u got destroyed in the fourteenth minute";
                          }
                        }
                        else {
                          echo "u got destroyed in the thirteenth minute";
                        }
                      }
                      else {
                        echo "u got destroyed in the twelvth minute";
                      }
                    }
                    else {
                      echo "u got destroyed in the eleventh minute";
                    }
                  }
                  else {
                    echo "u got destroyed in the tenth minute";
                  }
                }
                else {
                  echo "u got destroyed in the ninth minute";
                }
              }
              else {
                echo "u got destroyed in the eight minute";
              }
            }
            else {
              echo "u got destroyed in the seventh minute";
            }
          }
          else {
            echo "u got destroyed in the sixt minute";
          }
        }
        else {
          echo "u got destroyed in the fifth minute";
        }
      }
      else {
        echo "u got destroyed in the fourth minute";
      }
    }
    else {
      echo "u got destroyed in the third minute";
    }
  }
  else {
    echo "u got destroyed in the second minute";
  }
}
else {
  echo "u got destroyed in the first minute";
}
}

代码格式不正确时,很难发现语法错误。最后的}太多了。

我还要补充一点,这段代码是完全不可接受的吗?我什至不知道您要在此处解决哪个问题。您关于“没有其他方法”的说法是不正确的,总是有许多其他方法可以解决问题。

我确实知道我们每个人都需要学习某种方式,一开始我们努力使一切都变得可行。编码并不容易,正确的编码始终很难。只需为自己制定一个简单的规则:“我再也不会嵌套许多if语句!”,您会没事的。