【问题标题】:PHP: Loop LogicPHP:循环逻辑
【发布时间】:2013-07-31 16:44:22
【问题描述】:

我意识到这非常简单,但我觉得我忽略了一些东西。 我想要屏幕显示的是


RED This is 0.

GREEN This is 1.

让它在显示的文本之间来回交替。我的逻辑如果可以在红色和绿色之间交替使用,但是“This is 0”和“This is 1”文本没有显示。

这是我目前的代码:

<?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++;
}

?>

我意识到获得我想要的最简单的方法是:

<?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++;
}

?>

但是这段代码不包含我继续这个项目所需的逻辑。 对于为什么我的第一个代码没有打印“这是 0”的答案,我将不胜感激。

谢谢!

【问题讨论】:

  • 你在寻找什么逻辑?您已经回答了自己的问题

标签: php loops if-statement while-loop logic


【解决方案1】:

您正在寻找的是模组数量。

改变你的循环:

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

模运算符返回除法的余数。 见:What are the practical uses of modulus (%) in programming?

【讨论】:

    【解决方案2】:

    为什么不这样:

    $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。如果任一评估恰好为真,您将最终陷入无限循环。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我真的很感激。谢谢。
      【解决方案4】:

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

      问题出在这里:

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

      一旦进入第一个循环,它就会一直回显"This is 0.&lt;br&gt;",因为$a_count 是不变的。

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

      【讨论】:

        【解决方案5】:

        好吧,在 while($count

        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 循环。对你想做的事情很有用。模数也可以帮助您。

        【讨论】:

        • 不幸的是,这不是我得到的这就是我得到的是:红色绿色绿色绿色绿色绿色绿色绿色
        • 我还在一开始声明变量时初始化了 $count。
        • 你有吗? : $array = array(0=>"RED",1=>"GREEN"); $a_count = 0; $计数 = 0;我复制/粘贴您的第一个代码,并在开头添加此代码。
        【解决方案6】:

        我认为可能最简单的方法就是使用 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++;
        }
        

        【讨论】:

          猜你喜欢
          • 2012-02-24
          • 2016-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-17
          • 2015-06-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多