解题思路:
一行一行输出,输出完换行就行了。
具体看代码,写得很详细了。
注意事项:
有一个大坑!!!为什么会格式错误就是因为第一个三角形前面是要有m个空白字符的!!!
参考代码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int temp = n-1; //第一个三角形中空白字符
int temp1 = 1; //第一个三角形中*的数量,为2*temp1 - 1
int temp2 = n; //第二个三角形中*的数量,为2*temp2 - 1
for (int i = 1; i <=n ; i++) //控制行数
{
for (int j = 1; j <= m; j++) //这里是重点,是坑!!
{ //第一个三角形前面必须要有m个空白字符!!!
System.out.print(" ");
}
for (int j = 1; j <= temp; j++){
System.out.print(" ");
}
for (int j = 1; j <= 2*temp1-1; j++)
{
System.out.print("*");
}
for (int j = 1; j <= m; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= 2*temp2-1; j++)
{
System.out.print("*");
}
System.out.println();
temp--;
temp1++;
temp2--;
}
}
}