求打印三角形的代码
要求控制台打印出*
* *
* * *
* * * *
* * * * *
搜索更多相关主题的帖子:
代码 打印 三角形
----------------解决方案--------------------------------------------------------
#include<stdio.h>
int main()
{
char m='*';
printf("%6c\n%5c%2c\n%4c%2c%2c\n%3c%2c%2c%2c\n%c%2c%2c%2c%2c\n"
,m,m,m,m,m,m,m,m,m,m,m,m,m,m,m);
}
----------------解决方案--------------------------------------------------------
用JAVA代码实现
----------------解决方案--------------------------------------------------------
程序代码:
public class Triangle {
public static void main(String[] args) {
printTriangle(5);
}
public static void printTriangle(int line) {
for (int i = 0 ;i <= line ;i++) {
for (int j = 0; j < line - i; j++)
{
System.out.print(" ");
}
for (int x = 0;x < 2*i-1; x++ )
{
if(x%2==0)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
public static void main(String[] args) {
printTriangle(5);
}
public static void printTriangle(int line) {
for (int i = 0 ;i <= line ;i++) {
for (int j = 0; j < line - i; j++)
{
System.out.print(" ");
}
for (int x = 0;x < 2*i-1; x++ )
{
if(x%2==0)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
----------------解决方案--------------------------------------------------------