0

我要解决的问题是我有 16 种不同的动物,我需要编写一个 PHP 脚本来询问用户 4 个是/否问题,每个问题都会缩小可用动物的范围,直到最终显示答案。这意味着下一个问题将取决于用户在上一个问题中回答的内容。您知道如何在不使用大量 if else 语句的情况下做到这一点。

以下是我到目前为止所做的,尚未完成,但如果我继续使用 if 语句,我最终会得到太多,必须有更好的方法来做到这一点。有人建议我在数组中使用数组,但这对我没有帮助。任何帮助将非常感激。

<?php
session_set_cookie_params(2592000);
session_start();
?>

<html>
<head>
<title>Creature Guessing Game</title>
</head>
<body>
<h1>Creature Guessing Game</h1>
<p> Welcome to the creature guessing game! </p>
<p>Click the button 

below to start or restart the game </p>

<form method="post" action="Creatures.php">
<input type='submit' name='start' value='Start Game' />
</form>
 <?php
//If form not submitted, display form.
if (!isset($_POST['start'])){
?>
<?php
} //If form is submitted, process input

else{ //Question 1
echo "<p>Does the creature live mainly on the land?</p>";
echo "<form method='post' action='Creatures.php'>
<input type='submit' name='yes1' value='Yes' />
<input type='submit' name='no1' value='No' />
</form>";
}
if ($_POST['yes1']){ //Q1 - Yes
echo "<p>Does the creature have wings?</p>";
echo "<form method='post' action='Creatures.php'>
<input type='submit' name='yes2' value='Yes' />
<input type='submit' name='no2' value='No' />
</form>";
}
elseif($_POST['no1']){ //Q1 - No
echo "<p>Does the creature live in the water?</p>";
echo "<form method='post' action='Creatures.php'>
<input type='submit' name='yes3' value='Yes' />
<input type='submit' name='no3' value='No' />
</form>";
}
if ($_POST['yes2']){ //Q1 - Yes and Q2 - Yes
echo "<p>Can the creature fly?</p>";
echo "<form method='post' action='Creatures.php'>
<input type='submit' name='yes4' value='Yes' />
<input type='submit' name='no4' value='No' />
</form>";
}
elseif($_POST['no2']){ //Q1 - Yes and Q2 - No
echo "<p>Is the creature an insect?</p>";
echo "<form method='post' action='Creatures.php'>
<input type='submit' name='yes5' value='Yes' />
<input type='submit' name='no5' value='No' />
</form>";
}
if ($_POST['yes3']){ //Q1 - No and Q2 - Yes
echo "<p>Is the creature a reptile?</p>";
echo "<form method='post' action='Creatures.php'>
<input type='submit' name='yes6' value='Yes' />
<input type='submit' name='no6' value='No' />
</form>";
}
if ($_POST['no3']){ //Q1 - No and Q2 - No
echo "<p>Does the creature have feathers?</p>";
echo "<form method='post' action='Creatures.php'>
<input type='submit' name='yes7' value='Yes' />
<input type='submit' name='no7' value='No' />
</form>";
}
if ($_POST['yes4']){ //Q1 - Yes and Q2 - Yes and Q3 - Yes
echo "<p>Is the creature your thinking of white?</p>";
echo "<form method='post' action='Creatures.php'>
<input type='submit' name='yes8' value='Yes' />
<input type='submit' name='no8' value='No' />
</form>";
}
if ($_POST['yes8']){ //Answer 1: Q1 - Yes and Q2 - Yes and Q3 - Yes and Q4 - Yes 
echo "<p>Its a goose!</p>";
}
if ($_POST['no8']){ //Answer 2: Q1 - Yes and Q2 - Yes and Q3 - Yes and Q4 - No
echo "<p>Its a hawk!</p>";
}
?>
</body>
</html>
4

2 回答 2

1

这里不是 php 开发人员,但您需要做的是拥有一个Animal具有一组属性的类或对象。这些属性应该是Boolean(真/假)值,直接映射回您所询问的问题。这些Animal对象应该放入Collection(aList或 a Set)中。处理完每个问题后,调用一个函数,该函数删除所有布尔属性与用户提供的值不匹配的动物。例如:

Animal cat = new Animal();  
cat.canWalkOnLand = true;  

userDoesAnimalWalkOnLand = false;  
for(Animal in AnimalCollection)  
{  
    if(Animal.canWalkOnLand != userDoesAnimalWalkOnLand)  
    {  
       AnimalCollection.remove(Animal);
    }  
}  

这应该足以让您入门。(冗长的变量名帮助你理解)

于 2012-05-24T16:53:36.113 回答
0

您可以创建一系列问题,并使用 switch 语句和一个小函数,一遍又一遍地重复相同的废话,只接受您的问题作为参数 - 它会清理您的代码很多。

至于仅在您列出的两种动物之间扩展此游戏,除了我的之外,强烈推荐 Woot4Moo 的建议。

于 2012-05-24T16:56:05.297 回答