编写程序,提示用户输入一个整数,然后显示一个金子塔形状的图案

import java.util.Scanner;
public class chapt3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of lines:");
int n = input.nextInt();
int i = 1,m = 1;
int s = 2,k = 1;
for(m = 1;m<=n;m++){
for (k=1;k<=(n-m);k++)
System.out.print(" "); //循环输出n-m空格
for (i=m;i>=1;i--)
System.out.print(i);   //输出i,i-1,..1
for (s=2;s<=m;s++)
System.out.print(s);   //输出2,3,...,m
for (k=1;k<=n-m;k++)
System.out.print(" "); //输出n-m个空格
System.out.print("\n");    //换行
}

}

}

输出样式:

假设输入4,会输出:

      1    

    212

  32123

4321234




import java.util.Scanner;

public class Demo1 {



public static void main (String[] args) {
Scanner input = new Scanner(System.in);
        int number = input.nextInt();
        for (int n = 1; n <= number; n++){
        for (int k = number - n; k >= 1; k--) 
        System.out.print(" ");  //循环输出前半部分空格
        for (int i = 1; i <= n; i++)
        System.out.print(2 * i - 1);
        for (int j = n - 1; j >= 1; j--)
        System.out.print(2 * j - 1);
        for (int m =1; m <= n -1; m++)
        System.out.print(" "); //循环输出后半部分空格
        System.out.println();    //换行
        }
     
}

}

如输入3

输出样式

    1

  131

13531


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值