Home  >  Article  >  php教程  >  用*打印一个空的三角形

用*打印一个空的三角形

PHP中文网
PHP中文网Original
2016-05-25 16:58:081944browse

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

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

                   

                   

Statement:
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