0

I realize this is extremely simple but I feel I'm over-looking something. What I want to screen to display is


RED This is 0.

or

GREEN This is 1.

And for it to alternate back and forth between the displayed text. My logic if fine for alternating between Red and Green, but the "This is 0" and "This is 1" text is not displaying.

Here is my code so far:

<?php

$array = array(0=>"RED",1=>"GREEN");
$a_count = 0;
$count = 0;


while($count<10)
// DO 9 TIMES
{

    echo $array[$a_count] . ' ';
    //SUDO FOR IMAGE BEING DISPLAYED

    while($array[$a_count] == 0)
    {
        echo "This is 0.<br>";
    }

    while($array[$a_count] == 1)
    {
        echo "This is 1<br>";
    }


//<----SWITCH BACK AND FORTH---->
    if($a_count == 1)
    {
        $a_count = 0;
    }
    else
    {
        $a_count++;
    }
//<----------------------------->
    $count++;
}

?>

I realize the easiest way to get what I would like is:

<?php

$array = array(0=>"RED",1=>"GREEN");
$a_count = 0;
$count = 0;


while($count<10)
// DO 9 TIMES
{

    echo $array[$a_count] . ' ';
    //SUDO FOR IMAGE BEING DISPLAYED


//<----SWITCH BACK AND FORTH---->
    if($a_count == 1)
    {
        echo "This is 1<br>";
        $a_count = 0;
    }
    else
    {
        echo "This is 0.<br>";
        $a_count++;
    }
//<----------------------------->
    $count++;
}

?>

But this code does not contain the logic I need for the continuation of this project. I would greatly appreciate an answer as to why my first code is not printing "This is 0."

Thank you!

4

6 回答 6

3

您正在寻找的是一个 mod 计数。

改变你的循环:

for($i=0;$i<10;$i++){
    echo $array[$i%2].' This is '.($i%2);
}

模运算符返回除法的余数。请参阅:模数(%)在编程中的实际用途是什么?

于 2013-07-31T16:51:10.180 回答
1

为什么不这样:

$colors = array(0 => 'Red', 1 => 'Green');
$idx = 0;
$count = 0;
while($count < 10) {
    echo "The color is {$colors['$idx']}<br />";
    $count = 1 - $count; // if $count is 1, it becomes 0. if it's 0, it becomes 1
}

您的 while() 循环基本上完全没用。您正在尝试再次比较 RED 和 GREEN 字符串 0。如果任一评估恰好为真,您将最终进入无限循环。

于 2013-07-31T16:48:00.460 回答
0

我认为可能最简单的方法就是使用 switch 语句。我真的很傻,之前没有想到它。

while($count<10)
{

    echo $array[$a_count] . ' ';
    //PSUEDO FOR IMAGE BEING DISPLAYED

    switch($a_count):
    {
        case 1:
            echo "This is RED.<br>";
            $a_count = 0;
            break;

        case 0:
            echo "This is GREEN.<br>";
            $a_count++;
            break;

    }

    $count++;
}
于 2013-08-01T14:56:49.643 回答
0

您没有更改$a_countwhile 循环中的值,因此它们无法结束。

问题在这里:

while($array[$a_count] == 0)
{
    echo "This is 0.<br>";
}

while($array[$a_count] == 1)
{
    echo "This is 1<br>";
}

"This is 0.<br>"一旦它进入第一个循环,它将保持 echoing$a_count不变。

看起来您可以将这些whiles 更改为ifs 以使您的代码以您想要的方式工作。您可能还想检查$a_count是 0 还是 1,而不是$array[$a_count]

于 2013-07-31T16:48:04.360 回答
0

好吧,在 while($count<10) 之前的第一个示例中,您是否初始化了您的值?如果你这样做,你必须有这样的显示:

RED This is 0.0
This is 0.0
This is 0.0
This is 0.0
This is 0.0
...

"This is 0.0" 无限循环显示。

    while($array[$a_count] == 0)
    {
        echo "This is 0.$count<br>";
    }

    while($array[$a_count] == 1)
    {
        echo "This is 1<br>";
    }

您必须在 while 循环中更改该值。

其他提示,我认为你必须看看“foreach”php 循环。对你想做的事情很有用。模数也可以帮助你。

于 2013-07-31T17:00:30.270 回答
0

忽略此脚本的低效率,您的 while 循环将与数组的值进行比较,而不是与它的键进行比较。但是解决这个问题实际上会暴露另一个问题——一个无限循环。

于 2013-07-31T16:52:05.260 回答