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

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


你可能感兴趣的:(java)