Home > php教程 > PHP源码 > body text

用*打印一个空的三角形

PHP中文网
Release: 2016-05-25 16:58:08
Original
1939 people have browsed it

1. [文件]     1.php                                        

<?php
$n=5;
for($i=1;$i<=$n;$i++){
	//在打印*前,先打印空格
	for($k=1;$k<=$n-$i;$k++){
		echo" ";
	}
	//内层控制每层*的个数
	for($j=1;$j<=($i-1)*2+1;$j++){
		//如果是第一层和最后一层
		if($i==1||$i==$n){
			echo &#39;*&#39;;
		}else{
			if ($j==1||$j==($i-1)*2+1) {
				echo"*";
			}else{
			echo " ";
			}
		}
	}
	echo&#39;<br/>&#39;;
}
?>
Copy after login

2. [PHP]代码

<?php
$n=5;
for($i=1;$i<=$n;$i++){
	//在打印*前,先打印空格
	for($k=1;$k<=$n-$i;$k++){
		echo" ";
	}
	//内层控制每层*的个数
	for($j=1;$j<=($i-1)*2+1;$j++){
		//如果是第一层和最后一层
		if($i==1||$i==$n){
			echo &#39;*&#39;;
		}else{
			if ($j==1||$j==($i-1)*2+1) {
				echo"*";
			}else{
			echo " ";
			}
		}
	}
	echo&#39;<br/>&#39;;
}
?>
Copy after login

                   

                   

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template