什么是 PHP 中的模式编程?在屏幕上打印某种图案是一门编程艺术。这可以是形成模式的一系列数字、字母或特殊字符。最简单的模式示例是斐波那契数列(1、1、2、3、5、8、13、21、34 等)。还有其他在屏幕上设计的图案,比如星星金字塔。所以,基本上,模式编程只是在屏幕上打印一个模式。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
对于本文,我们将使用 PHP 来编码模式。不过别担心。一旦掌握了它的窍门,就会发现语法因语言而异。逻辑总是一样的。
所以,记住所有这些,现在让我们尝试对模式进行编码。
这是最简单的打印图案。它在后续行中打印越来越多的星星。第一行 1 颗星,第二行 2 颗星,依此类推。
让我们用五行代码来编写这个模式。牢记逻辑,我们的外循环将运行五次。由于每行中的星星数量直接取决于行号,因此我们的内部循环将是外部循环中控制变量的函数。让我们看看如何。
雷雷等等。这就是我们如何根据外循环控制变量来控制内循环。现在让我们看看该计划正在运行。
代码:
雷雷输出:
这与星半金字塔类似,只不过星星是右对齐的。
为了实现正确的缩进,我们将使用空格,然后打印星号。因此,会有两个内部循环——一个控制空格的数量,另一个控制星星的数量。
注意:请记住一件事,k 循环中的空格数是双倍空格。这是因为我们还随星星一起打印了一个空格。这为我们的图案提供了完整的外观,而不是拥挤的印花。当我们打印完整的金字塔时,我们将利用这一点。代码:
雷雷输出:
对于这个金字塔图案,星星的数量随着每条新线而不断减少。第一行有 5 颗星,第二行有 4 颗星,依此类推。
记住逻辑,我们知道外循环必须控制行数,内循环必须控制星数。这个逻辑是无法改变的。尽管如此,可以改变的是我们如何开始循环,增加或减少顺序。这意味着我们可以从 0 到 5 循环,也可以从 5 到 0 按降序循环。因此,对于像这样的倒置模式,我们知道第一行的星星数量更多。所以,我们选择减少订单循环。
代码:
雷雷输出:
This pattern is an indented inverted half pyramid. The number of stars decreases with each line and stars are right-aligned.
I believe by now you would be able to guess the logic for this one.
Code:
0; $i--) { // inner loop handles indentation for($k = $i; $k < $num; $k++ ) { // Print stars echo " "; } // inner loop handles number of stars for($j = 0; $j < $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
This pattern prints the full pyramid. Or in other words, it prints a triangle of stars on the screen.
This pattern is essentially a combination of Half pyramid and its mirror. Although there is a slight twist when we code it. Revisit the Note in Mirrored Half Pyramid. Remember, how we used double spacing to give a finished look to our pattern? Here we would use single spacing so that the stars are alternately aligned in odd and even number of rows, giving us a true triangular pattern.
Code:
$i+1; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j <= $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
This pattern prints a complete diamond shape on the screen. The number of stars increases until the maximum defined and then decrease back to 1, giving us a full diamond shape.
To print this pattern, we would need to divide the pattern into two halves. The upper half – which prints the increasing number of stars. The lower half– which prints the decreasing number of stars. To print both the halves, we would use two outer loops and corresponding inner loops.
Code:
$i+1; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j <= $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } // The Lower Half Pattern // Outer loop handles number of rows for ($i = $num-1; $i > 0; $i--) { // inner loop handles indentation for($k = $num-1; $k >= $i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j < $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
For this number pattern, we will print the numbers in relation to the row number. Thus, digit 1 would be printed once, 2 twice, 3 thrice and so on.
If you would have followed this tutorial line by line, by now you must have understood very well the working of nested loops to print patterns. This pattern also follows the same logic. Instead of stars, we print numbers. Now you ask how do we get the numbers? The answer is- simply through our control variablesiand j.
Code:
$i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 1; $j <= $i; $j++ ) { // Print numbers echo $i." "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
In this pattern, we would print the alphabets ABCDE in a pattern. Starting with A, the subsequent rows would introduce a new alphabet sandwiched between the previous alphabets.
The only trick in this pattern is to get the characters from our control variables. We do this by leveraging the ASCII value of the characters. The ASCII value ofAtoZis65to90. So, we calculate the ASCII value in each iteration and print the corresponding character. The chr() function in PHP is used to print a character from the ASCII code.
Code:
$i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 1; $j <= $i; $j++ ) { // Print characters echo chr(64+$j)." "; } for($j = $i-1; $j >= 1; $j-- ) { // Print characters echo chr(64+$j)." "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
Print for full alphabets and the pattern looks pretty cool.
This pattern is a dynamic pattern that prints the hourglass relative to the time elapsed, not an actual calculation though. For e.g., if one hour has elapsed, it will print one line of 0s in the upper half and one line of 1s in the lower half.
Code:
0; $i--) { // inner loop handles indentation for($k = $num; $k > $i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j < $i; $j++ ) { // Print characters if($num-$i < $hour) echo "0 "; else echo "1 "; } // go to new line after each row pattern is printed echo "\n"; } for ($i = 1; $i < $num; $i++) { // inner loop handles indentation for($k = $num-1; $k > $i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j <= $i; $j++ ) { // Print characters if($num-$i <= $hour) echo "1 ";else echo "0 "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 8; $hour = 3; print_pattern($num, $hour); ?>
Output:1 hour has elapsed.
Output:2 hours have elapsed.
Output:3 hours have elapsed.
And so on.
There is a lot to play with patterns. It’s all about keeping the code logic in mind. Once you get the code logic completely understood, there is no pattern you can’t print.
以上是PHP 中的模式的详细内容。更多信息请关注PHP中文网其他相关文章!