如何使用开关设置多个条件?

时间:2019-01-25 07:29:38

标签: php

我想在php上编写一个计数游戏。
规则如下:
1.玩家从0〜3中选择一个数字
2.电脑从玩家的选择中选择一个数字+ 0〜3 3.一到30输掉比赛

我编写的代码:

<?php  
$total = $_POST['total']; 
$user_number = $_POST['number']; // 1~3
$total='';
switch ($total){
case '4':
case '8':
case '12':
case '16':
case '20':
case '24':
case '28':
    $computer_selection = 1;
    break;
case '3':
case '7':
case '12':
case '16':
case '20':
case '23':
case '27':
    $computer_selection = 2;
    break;
case '2':
case '6':
case '11':
case '15':
case '19':
case '22':
case '26':
    $computer_selection = 3;
    break;
default:
    $computer_selection =1;
    break;
}
$computer_number = $total + $computer_selection;
$total = $user_number + $computer_number;
?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>恐怖の30ゲーム</title>
</head>
<body>
<?php print_r('The choice of computer'.$computer_number.'<br>'); ?>
<?php print_r('Current number:'.$total.'<br>'); ?>
恐怖の30
<?php
$total_nubmer1=$total+1;
$total_number2=$total+2;
$total_number3=$total+3;
?>
<form method='post' action='index2.php'>
    you:
    <select name='number'></br>
        <option value='<?php echo $total_nubmer1 ?>'><?php echo
$total_nubmer1 ?></option>
        <option value='<?php echo $total_number2 ?>'><?php echo 
$total_number2 ?></option>
        <option value='<?php echo $total_number3 ?>'><?php echo 
$total_number3 ?></option>
    </select>
    <input type='hidden' name='total' value='<?php $total?>'>
    <input type='submit' value='登録'>
</form>
</body>
</html>
<?php 
if ($user_number>=30){
echo "<script language=\"JavaScript\">alert(\"you lose\");location.href='index.php';</script>";
}else if($user_number<=29 && $user_number>28||$total>29){
echo "<script language=\"JavaScript\">alert(\"you win\");location.href='index.php'</script>";
}
?>

我不明白为什么computer_selection的输出总是变为默认值。

2 个答案:

答案 0 :(得分:0)

因为您将localstorage放在了切换之前。 因此,$ total始终在您的开关默认情况下输入。

答案 1 :(得分:0)

您正在重新分配void clearCache() async{ var appDir = (await getTemporaryDirectory()).path; new Directory(appDir).delete(recursive: true); 的值,然后再输入$total。您要做的另一件事是假设$ _POST参数中接收了switch的值,您应该检查是否首先设置了该值。因此,请删除$total行,并在您的作业中执行类似的操作

$total = '';

数字相同。这样,如果什么也没收到,它将成为默认值。

希望有帮助!