简体   繁体   English

PHP坚持循环练习

[英]PHP stuck with the loop excercise

I am stuck with this php loop.我被这个 php 循环困住了。 Then n = 3, k = 5, s = 21. Can anyone help me please ?然后 n = 3, k = 5, s = 21. 谁能帮帮我?

Rows 3. In the first row 5 chairs:
1 row: ⑁⑁⑁⑁⑁ (5 chairs)
2 row: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
3 row: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Chairs in total: 21

<?php
 for ($i=0; $i<=6; $i++) {
      for ($j=0; $j<$i; $j++) {
         if($i == 1) {
             echo ''; 
         }else{
               echo '&#9281'; 
             } 
        } echo ' '; 
      }
 ?> 

I think you want to use 'if' syntax.我认为您想使用“if”语法。 Is it right?这样对吗? If it's not correct, You should describe your work more detail如果不正确,您应该更详细地描述您的工作

<?php
// ...

for($i = 0; $i< sizeof($rows); $i++ ){
  if ($i % 2 == 0) {
    print "It's even";
  }
}

// ... If a row is an Array, use this one.

for($i = 0; $i< sizeof($rows); $i++ ){
  for($j = 0; $j< sizeof($rows[$i]); $j++ ){
    if ($j % 2 == 0) {
      print "It's even index of a row";
      //do Something.
    }
  }
}
?>

Example: When n = 3 and k = 8, it must be obtained that order s = 30 chairs.示例:当 n = 3 且 k = 8 时,必须得到订单 s = 30 把椅子。

Create a PHP solution that specifies the N-row queue in the variables, K-how many chairs should be in the first row.创建一个 PHP 解决方案,在变量中指定 N 行队列,K-第一行应该有多少把椅子。

For example: N = 3;例如:N = 3; K = 5; K = 5;

After loading the page (after performing the program actions with the variables available) the following should be displayed:加载页面后(使用可用变量执行程序操作后)应显示以下内容:

Rows 3. First row 5 chairs: Queue 1: ⑁⑁⑁⑁⑁ (5 chairs) Queue 2: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs) Queue 3: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs) Total required chairs: 21排 3. 第一排 5 把椅子: 队列 1:⑁⑁⑁⑁⑁(5 把椅子) 队列 2:⑁⑁⑁⑁⑁⑁⑁(7 把椅子) 队列 3:⑁⑁⑁⑁⑁⑁9⑁⑁⑁⑁所需椅子总数:21

If I understand correctly what you want, $n is number of row you needed, $k number of chair at row 1.如果我正确理解您想要什么,$n 是您需要的行数,第 1 行的 $k 椅子数。

Each row chair is added by two.每排椅子增加两个。

<?php
$n = 3;
$k = 5;
for($i = 0; $i < $n; $i++) {
    if($i >= 1) {
        echo PHP_EOL;
    }
    echo str_repeat('-', $k + ($i * 2));
}

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

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