多组输入,一个整数(2~20),表示金字塔边的长度,即“*”的数量,,也表示输出行数。
针对每行输入,输出用“*”组成的金字塔,每个“*”后面有一个空格。
4
* * * * * * * * * *
5
* * * * * * * * * * * * * * *
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j < n - i - 1) { System.out.print(" "); //控制空格数即可 } else { System.out.print("* "); } } System.out.println(); } } in.close(); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); for(int i = 1; i <= n; i++) { //空格 for(int j = n; j > i; j--) { System.out.print(" "); } //符号* for(int l = 1; l <= i; l++) { System.out.print("* "); } //换行 System.out.println(); } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int n = in.nextInt(); for(int i=1;i<=n;i++){ for(int j=n-1;j>=i;j--){ System.out.print(" "); } for(int p=1;p<=i;p++){ System.out.print("* "); } System.out.println(); } } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int x=sc.nextInt(); for(int i=1;i<=x;i++){ for(int j=1;j<=x-i;j++){ System.out.print(" "); } for(int k=1;k<=i;k++){ System.out.print("* "); } System.out.println(); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); for (int i = 1; i <= n; i++) {//hang for (int j = n - i; j >= 1; j--) { System.out.print(" "); } for (int m = 1; m <= i; m++) { System.out.print("* "); } System.out.println(); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int num = scanner.nextInt(); for(int i = 0;i < num;i++){ for(int j = num - i ;j > 1 ;j--){ System.out.print(" "); } for(int k = 0;k <= i;k++){ System.out.print("* "); } System.out.println(""); } } } }
import java.util.*; public class Main{ public static void main(String love[]){ Scanner input=new Scanner(System.in); while(input.hasNextInt()){ int n=input.nextInt(); for(int i=0;i<n;i++){ for(int k=n-i;k>1;k--){ System.out.print(" "); } for(int j=0;j<=i;j++){ System.out.print("*"+" "); } System.out.println(); } } input.close(); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int n = sc.nextInt(); for(int i = 1; i <= n; i++){ for(int j = 1; j <= n-i; j++){ System.out.print(" "); } for(int k = 1; k <= i; k++){ System.out.print("* "); } System.out.println(); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { Integer number = sc.nextInt(); for(int n = 1;n<=number;n++){ for(int s = number-n;s>0;s--){ System.out.print(" "); } for(int i = n;i>0;i--){ System.out.print("* "); } System.out.println(""); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { //接受数字输入 int n = sc.nextInt(); String temp = String.join("", Collections.nCopies(n+1, " ")); for (int i = 1; i <=n; i++) { //输出n-i个空格" ",以及拼接字符串->在i个*中插入空格" " //Collections.nCopies(i, "*") 是复制i-1个*,如i=3,就是*** //下面的String.join方法是在字符串空隙中插入指定字符串,如***,变成* * * System.out.println(String.join("", Collections.nCopies(n-i, " "))+ String.join(" ", Collections.nCopies(i, "*"))+" "); } } } }