PHP- if和else语句没有显示正确的输出

时间:2017-11-19 22:45:06

标签: php html if-statement

<!DOCTYPE html>
<html lang="en">
<head>
     <title>Rock Paper Scissors Game</title>
</head>
<body>
<h1> Rock Paper Scissors Game</h1>

(第2页)这是一个涉及图像的石头剪刀游戏......图像确实在用户输入(岩石,剪刀或纸张)后出现但通常就像(输出=岩石与纸张的图像然后它会说你赢了!而不是你输了!或者当它的抽奖没有出现时)     

        $playerthrows = $_POST['playerthrows'];
        $Choosefrom= array('rock', 'paper', 'scissors');
        $Choice= rand(0,2);
        $Computer=$Choosefrom[$Choice];


if($playerthrows == 'rock')
    {
    echo '<img src="rock1.png" alt="rock" />';
    }

    if($playerthrows == 'paper')
    {
    echo '<img src="paper1.png" alt="paper" />';
    }

    if($playerthrows == 'scissors')
    {
    echo '<img src="scissors1.png" alt="scissors" />';
    }

    if($Computer == 0)
    {
        echo '<img src="paper2.png" alt="paper" />'; 
    }
    if($Computer == 1)
    {
    echo '<img src="scissors2.png" alt="scissors" />'; 
    }

    if($Computer == 2)
    {
    echo '<img src="rock2.png" alt="rock" />'; 
    }

我是否搞砸了if语句?

    if ($playerthrows == $Computer)
    {
    echo'<br />';
    echo 'Draw.';
    }
    else if($playerthrows == 'rock' && $Computer == 1 )
    {
    echo'<br />';
    echo 'You Win!';
    }
    else if($playerthrows == 'rock' && $Computer == 0 )
    {
    echo'<br />';   
    echo  'You Lose!';
    }
    else if($playerthrows == 'scissors' && $Computer == 2 )
    {
    echo'<br />';   
    echo  'You Lose!';
    }
    else if($playerthrows == 'scissors' && $Computer == 0 )
    {
    echo'<br />';   
    echo 'You Win!';
    }
    else if($playerthrows == 'paper' && $Computer == 2 )
    {
    echo'<br />';   
    echo 'You Win!';
    }
    else if($playerthrows == 'paper' && $Computer == 1 )
    {
    echo'<br />';   
    echo 'You Lose!';
    }



?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

在您的数组$Choosefrom中,您将其声明为array('rock', 'paper', 'scissors')

但在你的情况下,它被称为array('paper', 'scissors', 'rock'),这会导致结果错误

应该是

if($Computer == 0)
{
    echo '<img src="rock2.png" alt="rock" />'; 
}
if($Computer == 1)
{
    echo '<img src="paper2.png" alt="paper" />'; 
}

if($Computer == 2)
{
    echo '<img src="scissors2.png" alt="scissors" />'; 
}

另外,你应该把这个词放在$ Computer的if-else条件中,而不是它们的索引号

e.g。 else if($playerthrows == 'rock' && $Computer == 'paper' )

答案 1 :(得分:0)

每当我看到大量if条件(或大量重复的swich-case块)时,我通常会发现更精简/更清洁的解决方案是将逻辑转换为&#34;查找& #34;阵列。这将使您的代码更简洁,更易于阅读,更易于修改。这是“不要重复自己”的页面。手册。

代码:(Run this Demo to see it at work

$_POST['playerthrows']='rock';
$humanoutcomes=[
    'paper'=>['paper'=>'Draw','rock'=>'Win','scissors'=>'Lose'],
    'rock'=>['rock'=>'Draw','scissors'=>'Win','paper'=>'Lose'],
    'scissors'=>['scissors'=>'Draw','paper'=>'Win','rock'=>'Lose']
];
if(!isset($_POST['playerthrows']) || !in_array($_POST['playerthrows'],array_keys($humanoutcomes))){
    echo "Ooops, you didn't throw a valid option.  Try again.";
}else{
    $playerthrows=$_POST['playerthrows'];
    $computerthrows=array_rand($humanoutcomes);

    echo "You threw: <img src=\"{$playerthrows}1.png\" alt=\"{$playerthrows}\" />\n";
    echo "Computer threw: <img src=\"{$computerthrows}2.png\" alt=\"{$computerthrows}\" />\n";
    echo "You {$humanoutcomes[$playerthrows][$computerthrows]}";
}

可能的输出:

You threw: <img src="rock1.png" alt="rock" />
Computer threw: <img src="paper2.png" alt="paper" />
You Lose