这是一个1到100之间的随机数猜测游戏。
我被困在猜测数字部分。代码不起作用,不管我说的是什么,只是告诉我信息"你的号码更低!"。我需要帮助。
这是我到目前为止所做的。
<?php
$x = rand(1, 100);
$num = '';
if (isset($_POST['submit'])) {
if ($num < $x)
{
echo " Your number is lower! <br />";
} elseif ($num > $x)
{
echo " Your number is higher! <br />";
} elseif ($num == $x)
{
echo " Congratulations! You guessed the hidden number. <br />";
} else
{
echo " You never set a number! <br />";
}
}
?>
<p>
<form action="" method="post">
<input type="text" name="num">
<button type="submit" name="submit">Guess</button>
<button type="reset" name="Reset">Reset</button>
</form>
</p>
答案 0 :(得分:0)
您需要在提交表单后设置num。
if (isset($_POST['submit'])) {
$num = $_POST['num'];
if ($num < $x)
{
echo " Your number is lower! <br />";
} elseif ($num > $x)
{
echo " Your number is higher! <br />";
} elseif ($num == $x)
{
echo " Congratulations! You guessed the hidden number. <br />";
} else
{
echo " You never set a number! <br />";
}
}
答案 1 :(得分:0)
你总是会变得更低,因为你value
内没有任何$num
。所以,你需要指定$num
<?php
$x = rand(1, 100);
$num = '';
if (isset($_POST['submit'])) {
$num = $_POST['num']; // Add this to set value for $num variable
if ($num < $x)
{
echo " Your number is lower! <br />";
} elseif ($num > $x)
{
echo " Your number is higher! <br />";
} elseif ($num == $x)
{
echo " Congratulations! You guessed the hidden number. <br />";
} else
{
echo " You never set a number! <br />";
}
}
?>
答案 2 :(得分:0)
// value of $x is in between 1 and 100 means greater than 0 and lesser than 101
$x = rand(1, 100);
// $num value is empty and while you attempt to compare with this this will converted to zero
$num = '';
// thus $num<$x always returns true, thats the mistake you need to assign the submitted value to the $num variable like
$num=$_POST['num'];
// put above line just before the if($num<$x)
答案 3 :(得分:0)
如果你将它作为switch/case
语句使用,那将会容易得多:
$x = rand(1,1000);
if(isset($_POST['submit'])) {
$num = $_POST['num'];
switch($num) {
case ($num < $x):
echo " Your number is lower! <br />";
break;
case ($num > $x):
echo " Your number is higher! <br />";
break;
case ($num == $x):
echo " Congratulations! You guessed the hidden number. <br />";
break;
case '':
echo " You never set a number! <br />";
break;
}
}
答案 4 :(得分:0)
您已设置$num = ''
(修复空字符串)并将其与随机生成的数字进行比较。很明显,这不起作用。你必须在PHP中阅读$_POST['num']
。
然而,这不会像预期的那样有用。您需要将随机数存储在session中,或者更轻松地向表单添加隐藏字段。否则随机数将在每次尝试时改变。
答案 5 :(得分:0)
在 2021 年 6 月 22 日,我使用 PHP 编写了一个范围内的猜数 [0 .. aMaxIntValue] 示例 Web 应用程序。 我认为以下代码可能对您有所帮助。 代码保存在单个 PHP 文件中。 它生成 #4 HTML 页面 ...
第一个初始页面用于收集应用程序参数(例如要猜测的最大整数数)
第二页是主游戏页面,要求用户猜测密码或重置游戏。此页面显示了之前的用户猜测以及一些给用户的猜测提示
第三页用于通知用户输掉比赛(即他没有更多的尝试机会了)
第4页用于通知用户赢得游戏(即猜对了)
留给用户的尝试次数是使用值范围 [0 .. max] 计算的
要猜的秘密数字是一个随机生成的整数
这是 PHP + HTML 代码...
<? php ?>
<? php ?>
<?php
session_start();
error_reporting (E_PARSE | E_COMPILE_ERROR);
function ResetGame()
{
unset ( $_SESSION['theMaxNumber'] );
}
function InitGame()
{
$_SESSION['theNumberToGuess'] = mt_rand (0, $_SESSION['theMaxNumber']);
$_SESSION['theMaxNumberOfTries'] = floor ( log ($_SESSION['theMaxNumber'], 2) ) + 1;
$_SESSION['theUserTriesCount'] = 0;
$_SESSION['thePrevGuessesString'] = '';
$_SESSION['theUserGuess'] = 0;
}
function ComputeNumberOfTriesLeft()
{
return $_SESSION['theMaxNumberOfTries'] - $_SESSION['theUserTriesCount'];
}
function IsNoMoreTriesLeft()
{
return ComputeNumberOfTriesLeft() <= 0;
}
$aCanPlayGame = false;
$aUserSubmittedGuess = false;
$aIsNoMoreTriesLeft = false;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ( isset ($_REQUEST['playgame']) ) {
$_SESSION['theMaxNumber'] = intval($_REQUEST['theMaxNumber']);
// init game ...
InitGame();
$aCanPlayGame = true;
}
elseif ( isset ($_REQUEST['submituserguess']) ) {
$aCanPlayGame = true;
$aUserSubmittedGuess = true;
$_SESSION['theUserGuess'] = intval($_REQUEST['theUserGuess']);
}
elseif ( isset ($_REQUEST['resetgame']) ) {
ResetGame();
}
else {
ResetGame();
}
}
else {
ResetGame();
}
// check a play
$aUserShouldTryLower = false;
$aUserShouldTryHigher = false;
$aUserWins = false;
$aUserLooses = false;
if ($aCanPlayGame) {
$aIsNoMoreTriesLeft = IsNoMoreTriesLeft();
if ( ! $aIsNoMoreTriesLeft ) {
// user have tries left
if ($aUserSubmittedGuess) {
// check user guess ...
$aUserGuess = intval($_SESSION['theUserGuess']);
if ( $aUserGuess > $_SESSION['theNumberToGuess'] ) {
$aUserShouldTryLower = true;
}
elseif ( $aUserGuess < $_SESSION['theNumberToGuess'] ) {
$aUserShouldTryHigher = true;
}
else {
$aUserWins = true;
// also reset game
ResetGame();
}
// add the current guess to the prev guesses string
$_SESSION['thePrevGuessesString'] .= $_SESSION['theUserGuess'] . ' ';
// increase the user tries count
++ $_SESSION['theUserTriesCount'];
// check tries count
if ( ! $aUserWins ) {
$aIsNoMoreTriesLeft = IsNoMoreTriesLeft();
if ($aIsNoMoreTriesLeft) {
// this was the last try
// no more tries left
$aUserLooses = true;
// also reset game
ResetGame();
}
}
}
}
else {
// no more tries left
$aUserLooses = true;
// also reset game
ResetGame();
}
}
?>
<?php if ($aUserLooses): ?>
<!DOCTYPE html>
<html>
<head>
<title>Guess a Number</title>
</head>
<body>
<p>Sorry, you loose the game</p>
<p>the Number to Guess was <?php echo $_SESSION['theNumberToGuess']; ?></p>
<form method="post">
<input type="submit" name="resetgame" value="reset game">
</form>
</body>
</html>
<?php elseif ($aUserWins): ?>
<!DOCTYPE html>
<html>
<head>
<title>Guess a Number</title>
</head>
<body>
<p>Congrats, you Win the Game</p>
<p>the Number to Guess was <?php echo $_SESSION['theNumberToGuess']; ?></p>
<form method="post">
<input type="submit" name="resetgame" value="reset game">
</form>
</body>
</html>
<?php elseif ($aCanPlayGame): ?>
<!DOCTYPE html>
<html>
<head>
<title>Guess a Number</title>
</head>
<body>
<p>the Max Number is <?php echo $_SESSION['theMaxNumber']; ?></p>
<p>Guess a Number in the Range [ 0 .. <?php echo ($_SESSION['theMaxNumber']); ?> ]</p>
<p>[DEBUG] the secret number to guess is <?php echo $_SESSION['theNumberToGuess']; ?></p>
<p>you have <?php echo ComputeNumberOfTriesLeft(); ?> tries left</p>
<form method="post">
<label for="theUserGuess">Enter your Guess: </label>
<input type="text" id="theUserGuess" name="theUserGuess">
<input type="submit" name="submituserguess" value="Guess">
<input type="submit" name="resetgame" value="reset game">
</form>
<p>Prev Guesses: <?php echo $_SESSION['thePrevGuessesString']; ?> </p>
<p>
<?php
if ($aUserShouldTryLower) {
echo 'you should try a lower < guess';
}
elseif ($aUserShouldTryHigher) {
echo 'you should try a higher > guess';
}
else {
echo 'no guess';
}
?>
</p>
</body>
</html>
<?php else: ?>
<!DOCTYPE html>
<html>
<head>
<title>Guess a Number</title>
</head>
<body>
<p>Guess a Number from (0) to ... </p>
<form method="post">
<input id="theMaxNumber" name="theMaxNumber">
<input type="submit" name="playgame" value="play game">
</form>
</body>
</html>
<?php endif; ?>
<? php ?>
就是这样……