简体   繁体   English

每3次打印一次

[英]Every 3rd Time, Print This

<?php

$ohhai = 1;

while ($ohhai != 102)
{
    for($t = 1; $t < $ohhai; $t++)
    {
        //if ($t % 3 == 0)
        //{
            $e = $t / 3;
            if (is_int($e))
            {
                echo "*";

            }
        //}
    }

    echo("<br />");
    $ohhai++;
}

?>

What I'm trying to do is output the string every 3rd time, like this: 我想做的是每3次输出一次字符串,如下所示:

$t = 3;

*

$t = 6;

**

$t = 9;

***

and so on. 等等。 I tried numerous ways to obtain this, and this is the closest I got to it, and this is the closest I get. 我尝试了多种方法来获得此结果,这是我获得的最接近结果,这也是我获得的最接近结果。 What this prints out is located here (hard order to type out). 打印出来的内容位于此处(很难输入)。 How can I go about accomplishing the every 3rd time trick? 我该如何完成每三个时间的把戏?

/ gives you the quotient. /给出商。 Instead you need take % operator and check whether the result of % operation is 0 and then print the value. 相反,你需要拿%运营商和检查的结果是否%的操作是0,然后打印值。

<?php

$ohhai = 1;
$times = 1;   // This is variable that keeps track of how many times * needs to printed. It's fairly straight forward to understand, why this variable is needed.

while ($ohhai != 102)
{
    if ($t % 3 == 0)
    {
        for ( $counter = 0; $counter < $times; $counter++)
        {
            echo "*";
        }
        $times ++;
        echo("<br />");
    }
    $ohhai++;
}

?>
if($something % 3 == 0)
{
  //do something
}

% is the modulo operator, it returns the remainder of a division. %是取模运算符,它返回除法的余数。 If the result is 0, the division happened without a remainder. 如果结果为0,则除法运算没有余数。

You can use the modulus operator in most languages, ie the remainder of a divide operation 您可以在大多数语言中使用模运算符,即除法运算的其余部分

if(iterationNumber % 3 == 0) it's the third time. if(iterationNumber%3 == 0)这是第三次。

I'm a little unclear on what you're trying to do, but I suspect that what you're missing is the modulus operator, % . 我不清楚您要做什么,但是我怀疑您缺少的是模数运算符%

In PHP, x % y evaluates to the remainder you get when dividing x by y. 在PHP中, x % y等于除以y所得的余数。 So if you're counting something and you want to run some code for every third one, you can do: 因此,如果您要计数,并且想为每三个代码运行一些代码,则可以执行以下操作:

if (0 == $count % 3) { // action to perform on every third item }

See the PHP Manual at http://php.net/manual/en/language.operators.arithmetic.php for more information. 有关更多信息,请参见http://php.net/manual/en/language.operators.arithmetic.php上的PHP手册。

Also, I think you might want one more for loop so that you're printing out the right number of *s. 另外,我认为您可能还需要一个for循环,以便打印出正确数量的* s。

<?php

$ohhai = 1;

while ($ohhai != 102)
{

  // we only want to print on multiples of 3
  if( 0 == $ohhai % 3 ) {


   for($t = 1; $t <= $ohhai; $t++){
       echo "*";
   }

   echo("<br />\n");
  }
$ohhai++;
}

Use the modulo operator. 使用模运算符。

For example: 例如:

  1. 10 % 2 = 0 because 2 divides 10 without a remainder. 10 % 2 = 0因为2除以10不会得到余数。
  2. 10 % 3 = 1 because 3 divides 10 with a remainder of 1. 10 % 3 = 1因为3将10除以1。

So with your commented code, your script would look like this: 因此,使用注释的代码,脚本将如下所示:

<?php

$ohhai = 1;

while ($ohhai != 102)
{
    for($t = 1; $t < $ohhai; $t++)
    {
        if ($t % 3 == 0)
        {
            echo "*";
        }
    }

    echo("<br />");
    $ohhai++;
}

?>
$total=102;
for($i=1;$i<=$total;$i++) {
    if($i%3==0){
        for($j=1;$j<=($i/3);$j++)
            echo "*";
        echo "<br/>";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM