怎么用java编写金字塔?
2个回答
展开全部
public class King
{
public static void main(String argc[]) {
int t;
java.util.Scanner san = new java.util.Scanner(System.in);
System.out.print("请输入行数: ");
t = san.nextInt();
for (int i = 1; i <= t; i++) {
for (int f = 1; f <= (t - i); f++)
System.out.print(" ");
for (int ff = 1; ff <= (2 * i - 1); ff++)
System.out.print("*");
System.out.println();
}
}
}
{
public static void main(String argc[]) {
int t;
java.util.Scanner san = new java.util.Scanner(System.in);
System.out.print("请输入行数: ");
t = san.nextInt();
for (int i = 1; i <= t; i++) {
for (int f = 1; f <= (t - i); f++)
System.out.print(" ");
for (int ff = 1; ff <= (2 * i - 1); ff++)
System.out.print("*");
System.out.println();
}
}
}
展开全部
实现思路就是先定义一个二维的”金字塔“数组,之后通过空格控制输出位置即可。
public class wode {
public static void main(String[] args) {
char[][] a = { { 'A' }, { 'B', 'B', }, { 'C', 'C', 'C', 'C' },
{ 'D', 'D', 'D', 'D', 'D', 'D' } };
for (int i = 0; i < a.length; i++) {
for (int k = a.length - i; k > 0; k--)
System.out.print(" ");//添加空格
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j]);//输出值
}
System.out.println();//换行
}
}
}
public class wode {
public static void main(String[] args) {
char[][] a = { { 'A' }, { 'B', 'B', }, { 'C', 'C', 'C', 'C' },
{ 'D', 'D', 'D', 'D', 'D', 'D' } };
for (int i = 0; i < a.length; i++) {
for (int k = a.length - i; k > 0; k--)
System.out.print(" ");//添加空格
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j]);//输出值
}
System.out.println();//换行
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询