dongtiao6362 2016-05-11 00:01
浏览 52
已采纳

在数组中使用随机变量

First of all, sorry if this is worded poorly, as I'm a beginner at code.

I'm currently working on an online computer science course, however I'm quite confused on how to do one small part. We need to use arrays for this activity, where the user has multiple selections and each different selection has a different/unique text output. Everything works fine, except I need an option to choose a random selection, however I'm a bit confused on how to make it. You can see from my code the options 1-8. I want it to randomly select one of the selections.

Here's my code:

<?php
$train[0] = "Canada";
$train[1] = "Sahara Desert";
$train[2] = "Russia";
$train[3] = "Chernobyl";
$train[4] = "United States";
$train[5] = "North Korea";
$train[6] = "Germany";
$train[7] = "Hawaii";
?>

<!DOCTYPE html>
<html>
<head>
Took out everything here, it's not important.
</head>

    <body>
        <center>
    <h1>Vacation Time!</h1>

    <h4>You and your family just won the lottery! You all want to go on vacation, but nobody can agree where to go. Inside each train cart has a card with a location written on it. Whatever you find is where you're going! </h4>

        <form name="form1" action="activity-2-7-arrays-a.php" method="post">

            <label>Which cart on the train do you want to choose?</label>
            <br>
            <select name="cart" required>
                <option value="1">First Cart</option>
                <option value="2">Second Cart</option>
                <option value="3">Third Cart</option>
                <option value="4">Fourth Cart</option>
                <option value="5">Fifth Cart</option>
                <option value="6">Sixth Cart</option>
                <option value="7">Seventh Cart</option>
                <option value="8">Eight Cart</option>
                <option value="show">Show all options</option>
                <option value="any">Choose Randomly</option>
                <br>
            </select><br/>
            <input type="submit" name="subButton" class="subButton" value="Go!"/><br/>
            </form>

    <h1><u>Final Results</u></h1>

<?php
if($_POST['subButton']) {
    $cart = $_POST['cart'];
    $roll = rand(1,9);

    if($cart == show) {
        for($x = 1; $x <= 9; $x++) {
            echo "<p> You could have ender up in... </p>";
            echo "<h2> " . $train[$x] . "</h2>";
        }
        return;
    }
    echo "<h2>"."Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>";
}
return;

if ($cart == $roll) {

}
echo "<h2>"."Can't handle the pressure? You were selected to go to  " . $train[$roll] . "! Have fun! </h2>";
?>

I'm sure it's a bit messy, also. Hopefully you understand what I mean. If you're able to explain the answer to me that would be extremely helpful. Thank you :)

  • 写回答

4条回答 默认 最新

  • dongzhen7108 2016-05-11 00:53
    关注

    You are randomly generating a value regardless of the user choice and comparing it with user's selection and other weird things.

    <?php
    if($_POST['subButton']) {
        $cart = $_POST['cart'];
        $roll = rand(1,9); 
    

    You are generating a random value before even checking if user selected 'Choose randomly' and why generate a random between 1 and 9? Your $train array starts with index 0 and ends with index 7.

        if($cart == show) { 
    

    String needs to be quoted.

            for($x = 1; $x <= 9; $x++) {
    

    Again looping $x from 1 to 9 makes no sense because of your array indexes.

                echo "<p> You could have ender up in... </p>";
                echo "<h2> " . $train[$x] . "</h2>";
    

    Will fail when $x reaches 8 since last index in $train is 7.

            }
            return;
        } 
        echo "<h2>"."Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>";
        }
        return;
    

    So if user didn't select 'Show all options' you show him his chosen location. If user chose 'Select Randomly' this will fail since $cart would have value 'any' and $train['any'] does not exist.

    Here is code with correct logic.

    <?php
    if($_POST['subButton']) {
        $cart = $_POST['cart'];
        if ($cart == 'any') {// Check if user selected 'Choose Randomly'
            $roll = rand(0, 7);
            echo "<h2>"."Can't handle the pressure? You were selected to go to  " .  $train[$roll] . "! Have fun! </h2>";
        }
        else {
            if ($cart == 'show') { // If user selected 'Show all options'
                echo "<p> You could have ender up in... </p>";
                for($x = 0; $x <= 7; $x++) {
                    echo "<h2> " . $train[$x] . "</h2>";
                }
            }
            else { // User selected cart so show him chosen location
                echo "<h2>"."Well, it looks like you're going to " . $train[$cart] . "! Have fun! </h2>";
            }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?