0

私が解決しようとしている問題は、16種類の動物がいることです。ユーザーに、4つのはい/いいえの質問をするPHPスクリプトを作成する必要があります。各質問は、最終的に答えが表示されるまで、利用可能な動物を絞り込みます。これは、次の質問は、ユーザーが前の質問で何に答えたかによって異なることを意味します。たくさんのifelseステートメントを使用せずにこれを行う方法を知っていますか。

以下は私がこれまでに行ったことであり、終了していませんが、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

Animalここでは PHP 開発者ではありませんが、一連のプロパティを持つクラスまたはオブジェクトを作成する必要があります。これらのプロパティはBoolean、尋ねている質問に直接マップする (true/false) 値である必要があります。これらのオブジェクトは(aまたは a )Animalに入れる必要があります。各質問が処理された後、ブール値のプロパティがユーザーによって提供された値と一致しないすべての動物を削除する関数を呼び出します。例えば: CollectionListSet

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 ステートメントと、質問をパラメーターとして受け入れるだけで同じがらくたを何度も繰り返す小さな関数を使用できます。これにより、コードが大幅にクリーンアップされます。

あなたがリストした 2 匹の動物の間でこのゲームを拡張する限り、私の提案に加えて Woot4Moo の提案を強くお勧めします。

于 2012-05-24T16:56:05.297 に答える