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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀