我正在做一个对立测验。这个想法是当用一个词提示时。你必须反其道而行之。我制作了两个带有单词列表的数组和一组要问的预定义示例问题。这个想法是我将这个词与一个相反的词进行比较,它会继续下去,直到它为输入找到正确的词为止。
由于某种原因,它不起作用。我对 PHP 还很陌生,我相信有一种更简单的方法可以做到这一点。现在这就是我必须处理的。
我也想用数组来做这个,而不是用 MySQL
<?php
$wl1 = array('Hot', 'Summer', 'Hard', 'Dry', 'Simple', 'Light', 'Weak', 'Male', 'Sad', 'Win', 'Small', 'Ignore', 'Buy', 'Succeed', 'Reject', 'Prevent',
'Exclude');
$wl2 = array('Cold', 'Winter', 'Soft', 'Wet', 'Complex', 'Darkness', 'Strong', 'Female', 'Happy', 'Lose', 'Big', 'Pay Attention', 'Sell', 'Fail', 'Accept',
'Allow', 'Include');
$compl = array("Hot is to cold",
"Summer is to winter",
"Hard is to soft",
"Dry is to wet",
"Simple is to complex",
"Light is to darkness",
"Weak is to strong",
"Male is to female",
"Sad is to happy",
"Win is to lose",
"Small is to big",
"Ignore is to pay attention",
"Buy is to sell",
"Succeed is to fail",
"Reject is to accept",
"Prevent is to allow",
"Exclude is to include");
$complr = $compl[array_rand($compl)];
$wl2r = $wl2[array_rand($wl1)];
$q = $complr . " as ".$wl2r." is to "."<br>";
echo $q;
if(isset($_POST['submit'])){
$score = 0;
$answer = $_POST['answer'];
if($wl2r == "Cold" && $answer == "Hot"){
echo "Correct";
$score++;
}
if($wl2r == "Winter" && $answer == "Summer"){
echo "Correct";
$score++;
}
if($wl2r == "Soft" && $answer == "Hard"){
echo "Correct";
$score++;
}
if($wl2r == "Wet" && $answer == "Dry"){
echo "Correct";
$score++;
}
if($wl2r == "Complex" && $answer == "Simple"){
echo "Correct";
$score++;
}
if($wl2r == "Darkness" && $answer == "Light"){
echo "Correct";
$score++;
}
if($wl2r == "Strong" && $answer == "Weak"){
echo "Correct";
$score++;
}
if($wl2r == "Female" && $answer == "Male"){
echo "Correct";
$score++;
}
if($wl2r == "Happy" && $answer == "Sad"){
echo "Correct";
$score++;
}
if($wl2r == "Lose" && $answer == "Win"){
echo "Correct";
$score++;
}
if($wl2r == "Big" && $answer == "Small"){
echo "Correct";
$score++;
}
if($wl2r == "Pay Attention" && $answer == "Ignore"){
echo "Correct";
$score++;
}
if($wl2r == "Sell" && $answer == "Buy"){
echo "Correct";
$score++;
}
if($wl2r == "Fail" && $answer == "Succeed"){
echo "Correct";
$score++;
}
if($wl2r == "Accept" && $answer == "Reject"){
echo "Correct";
$score++;
}
if($wl2r == "Allow" && $answer == "Prevent"){
echo "Correct";
$score++;
}
if($wl2r == "Include" && $answer == "Exclude"){
echo "Correct";
$score++;
}
echo $score;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Opposites</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="form-group">
<form action="task3.php" method="post">
Enter your Answer <input type="text" name="answer">
<input type="submit" name='submit'>
</form>
</div>
</body>
</html>
一个问题是,如果没有数据库,就不可能在页面加载之间保留程序所需的状态数据。例如,在提交之后,无法知道在上一次加载页面时进行了哪个随机选择或得分是多少。执行此操作的最佳方法是使用数据库或 Ajax,但除此之外,还可以(但不是很优雅)使用隐藏的输入字段在不费力的情况下传递数据。您还可以探索本地存储和 JavaScript,它们不会那么笨拙。
至于您的游戏逻辑,请考虑使用关联数组。数组键不是数字序列 0、1、2...,而是使用第一对的字符串作为检索其伙伴的键。这消除了巨大的条件块、大量输入和可能的错误:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Opposites</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<?php
$compliments = [
'Hot' => 'Cold',
'Summer' => 'Winter',
'Hard' => 'Soft',
'Dry' => 'Wet',
'Simple' => 'Complex',
'Light' => 'Dark',
'Weak' => 'Strong',
'Male' => 'Female',
'Sad' => 'Happy',
'Win' => 'Lose',
'Small' => 'Big',
'Ignore' => 'Pay Attention',
'Buy' => 'Sell',
'Succeed' => 'Fail',
'Reject' => 'Accept',
'Prevent' => 'Allow',
'Exclude' => 'Include'
];
$score = isset($_POST['score']) ? (int)$_POST['score'] : 0;
if (isset($_POST['answer']) && isset($_POST['question'])) {
if ($_POST['answer'] === $compliments[$_POST['question']]) {
echo "<div>Correct, " . $_POST['question'] . " is to " . $_POST['answer'] . ".</div>";
$score++;
}
else {
echo "<div>Incorrect. " . $_POST['question'] . " is to " .
$compliments[$_POST['question']] . ", not to " . $_POST['answer'] . ".</div>";
}
}
$sample = array_rand($compliments);
$test = array_rand($compliments);
echo "<div>Score: $score</div>";
echo "<div>$compliments[$sample] is to $sample as $test is to </div>";
?>
<div class="form-group">
<form action="task3.php" method="post">
Enter your Answer <input type="text" name="answer">
<input type="hidden" name="question" value="<?= isset($test) ? $test : "" ?>"></input>
<input type="hidden" name="score" value="<?= isset($score) ? $score: "" ?>"></input>
<input type="submit" name='submit'>
</form>
</div>
</body>
</html>
此外,如果您想访问反向数组,例如查找'Cold'
get 'Hot'
,请使用array_flip。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句