繁体   English   中英

在php类中生成一个随机数

[英]Generate a random number in a php class

我正在做一个我在网上发现自己学习php的练习。 我正在制作的节目是一个“猜数字”游戏,我需要生成一个随机数来猜测。 当我没有使用类并使用html输入框进行用户输入时,我在练习中取得了成功:

<?php
if(isset($_POST['number'])) {
    $number = $_POST['number'];
} else {
    $number = rand(1,99);
}

if($_POST["guess"]) {
    // grab the user input guess
    $guess  = $_POST['guess'];
    $number  = $_POST['number'];
    $output = '';
    static $count = 0; //Initialize count

    if ($guess < $number) { 
        $output .= "$guess" . "<br />" . "Guess Higher";
        $count++;
    } elseif($guess > $number) {       
        $output .= "$guess" . "<br />" . "Guess Lower";
        $count++;
    } elseif($guess == $number) {  
        $count++;    
        $output .= "$guess" . "<br />" . "You got it!" . "<br />" . "You guessed it in " . $count . " trials";
    }
} ?>


<!DOCTYPE html>
<html>
    <head>
        <title>Guess A Number</title>
    </head>

    <body>
        <form action="NumberGuess.php" method="post">
            <label for="guess">Guess A Number:</label><br/ >
            <input type="text" name="guess" />
            <input name="number" type="hidden" value="<?= $number ?>" />
            <input name="submit" type="submit" />
            <br/>
            <?php echo $output ?>
            <br/>
        </form>
    </body>
</html>

但是,现在我想使用json作为输入并使用类/对象重复练习。 我尝试使用rand()生成随机数,但是它会导致问题并且页面不会加载:

class NumberGuess {
    protected $guess = 0;
    const randNumber = rand(1,99);
    protected $isNumber = false;
    protected $higher = true;

    function __construct($inputGuess) {
        $this->guess = $inputGuess;
    }

    public function getResult() {
        $this->numberChecker();

        if($this->higher == true) {
            $this->higher = "higher";
        } elseif($this->higher == false) {
            $this->higher = "lower";
        }

        if($this->isNumber == false) {
            return $this->guess . " is incorrect, " . "try guessing " . $this->higher;
        } elseif($this->isNumber == true) {
            return $this->guess . " is correct!";
        }
    }

    private function numberChecker() {
        if($this->guess < self::randNumber) {
            $this->higher = true;
            $this->isNumber = false;
        } elseif($this->guess > self::randNumber) {
            $this->higher = false;
            $this->isNumber = false;
        } elseif($this->guess == self::randNumber) {
            $this->higher = false;
            $this->isNumber = true;
        }
    }
}

if ( isset($_SERVER['QUERY_STRING']) ) {
    $inputString = $_SERVER['QUERY_STRING'];
    $inputGuess = intval($inputString);
}

$myGuess = new NumberGuess($inputGuess);
echo json_encode($myGuess->getResult());

有人可以向我解释如何使用json在类中生成随机数吗?

您可以使用PHP rand()mt_rand()函数在范围之间生成随机数。

rand()语法

int rand ( void )
#OR
int rand ( int $min , int $max )

mt_rand()语法

int mt_rand ( void )
#OR
int mt_rand ( int $min , int $max )

注意:您可以使用JavaScript Math.random()函数即时生成随机数。 或者您可以使用Ajax使用PHP生成随机数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM