在php类中生成一个随机数

时间:2017-04-06 01:59:11

标签: php json class object

我正在网上找到一个自学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在课堂上生成随机数吗?

1 个答案:

答案 0 :(得分:0)

您可以使用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生成随机数。