0

所以我做了这个功能,你可以随机选择一种颜色。问题是我要打印相同的函数 8 次,我不希望其中两个打印返回相同的变量。所以基本上我希望它在之前打印过颜色的情况下再次运行该功能。

function random_color(){
$color_numb = "8";

$color = rand(1, $color_numb);

if($color == "1"){
  $type = "Blue";
}
if($color == "2"){
  $type = "Red";
}
if($color == "3"){
  $type = "Orange";
}
if($color == "4"){
  $type = "Green";
}
if($color == "5"){
  $type = "Yellow";
}
if($color == "6"){
  $type = "Black";
}
if($color == "7"){
  $type = "Purple";
}
if($color == "8"){
  $type = "White";
}
return $type;

}
print random_color();
print random_color();
print random_color();
print random_color();
print random_color();
print random_color();
print random_color();
print random_color();

所以知道我怎么能做到这一点吗?

4

4 回答 4

3

如果您需要 8 种不同颜色的不同顺序,只需创建数组,用颜色填充它,然后用这个随机化(随机播放)数组 - http://php.net/manual/en/function.shuffle.php

$colors = array();
$colors[] = 'red';
$colors[] = 'green';
....
shuffle($colors);
于 2013-09-03T19:37:39.103 回答
0

Instead of your random_color() function and your 8 prints, try this :

$colors = array();
$colors[] = "Blue";
$colors[] = "Red";
$colors[] = "Orange";
$colors[]= "Green";
$colors[]= "Yellow";
$colors[] = "Black";
$colors[]= "Purple";
$colors[]= "White";
shuffle($colors);
for ($i = 0; $i < 8; $i++) {
   echo $colors[$i]
}
于 2013-09-03T20:09:25.633 回答
0

您也可以根据会话尝试此操作:

<?php
//put this line at top of the page before printing anything
session_start();

function random_color(){
  $color_numb = "8";

  $random = rand(1, $color_numb);
  $colors  = array(1 => "Blue", "Red", "Orange", "Green", "Yellow", "Black", "Purple", "White");

  $current_color = $colors[$random];

  if(!isset($_SESSION['oldcolor'])){
    $_SESSION['oldcolor'] = $current_color;
    return $current_color;
  }
  while($current_color == $_SESSION['oldcolor']){
   $current_color = $colors[rand(1, $color_numb)];
  }
  $_SESSION['oldcolor'] = $current_color;

  return $current_color;
}

echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
?>
于 2013-09-03T19:48:17.773 回答
0

如果您希望按需显示颜色,但从不显示重复的颜色,那么您可以这样做:

在这里工作 jsFiddle

HTML:

<input id="mybutt" type="button" value="Show Next Color" />
<input id="listall" type="button" value="List All Found Values" />

Javascript/jQuery:

var aCN = ['blue', 'red', 'orange', 'green', 'yellow', 'black', 'purple', 'white'];
var aFound = new Array();

$('#mybutt').click(function () {
    xx = 1;
    while (xx == 1) {
        getcol = random_color();
        if (aFound.indexOf(getcol) == -1) {
            alert(getcol);
            aFound.push(getcol);
            xx = 0;
        } else {
            if (aFound.length > 7) {
                alert('You already saw all values');
                xx = 0;
            }
        }
    }
});

$('#listall').click(function () {
    if (aFound.length == 0) {
        alert('Nothing seen yet...');
        return false;
    }
    Object.keys(aFound).forEach(function (key) {
        alert(aFound[key]);
    });
});

function random_color() {
    $color_numb = "8";
    $color = Math.floor(Math.random() * $color_numb);

    myCol = aCN[$color];
    return myCol + ' - ' + $color;
}
于 2013-09-03T20:24:33.753 回答