多组输入,一个整数(2~20)。
针对每行输入,输出用“*”组成的菱形,每个“*”后面有一个空格。
2
* * * * * * * * *
3
* * * * * * * * * * * * * * * *
4
* * * * * * * * * * * * * * * * * * * * * * * * *
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(); //上半边 n+1层:金字塔 for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (j < n - i) { System.out.print(" "); } else { System.out.print("* "); } } System.out.println(); } //下半边 n层:倒金字塔 for (int i = 0; i < n; i++) { for (int j = 0; j < i + 1; j++) { //空格要从1开始 System.out.print(" "); } for (int j = n - 1 - i; j >= 0; j--) { 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(); // 1 到 n + 1 for (int i = 1; i <= n + 1; i++) { //行 //空格 for (int j = n; j >= i; j--) { System.out.print(" "); } //符号 for (int l = 1; l <= i; l++) { System.out.print("* "); } //换行 System.out.println(); } // n + 2 到 2 * n + 1 for (int i = 1; i <= n; i++) { //行 //空格 for (int j = 1; j <= i; j++) { System.out.print(" "); } //符号 for (int l = n; 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+1;i++){ for(int j=n;j>i-1;j--){ System.out.print(" "); } for(int p=1;p<=i;p++){ System.out.print("* "); } System.out.println(); } for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ System.out.print(" "); } for(int p=n;p>i-1;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=x+1-i;j>=1;j--){ System.out.print(" "); } for(int k=1;k<=i;k++){ System.out.print("* "); } System.out.println(); } for(int i=1;i<=x+1;i++){ System.out.print("* "); } System.out.println(); for(int i=1;i<=x;i++){ for(int k=1;k<=i;k++){ System.out.print(" "); } for(int j=x+1-i;j>=1;j--){ 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(); //控制第1行到第num+1行(算是上半部分) for(int i = 0;i < num + 1;i++){ //控制空格 for(int j = num - i; j > 0 ;j--){ System.out.print(" "); } //控制星号 for(int k = 0;k <= i;k++){ System.out.print("* "); } System.out.println(""); } //控制第num+2到第2 * num + 1行(算是下半部分) for(int i = 0;i < num;i++){ //控制空格 for(int j = 0;j <= i;j++){ System.out.print(" "); } //控制星号 for(int k = num - i;k > 0 ;k--){ System.out.print("* "); } System.out.println(""); } } } }
import java.util.Scanner; /** * @Title: 菱形图案 * @Remark: KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的菱形图案。 * 输入描述: * 多组输入,一个整数(2~20)。 * * 输出描述: * 针对每行输入,输出用“*”组成的菱形,每个“*”后面有一个空格 * @Author: ijunfu * @Version: 1.0.0 * @Date: 2022-03-19 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNextLine()) { int row = Integer.parseInt(in.nextLine()); for(int i=0; i<row; i++) { for(int j=0; j<row-i; j++) { System.out.print(" "); } for(int j=0; j<=i; j++) { System.out.print("* "); } System.out.println(); } for(int i=0; i<=row; i++) { System.out.print("* "); } System.out.println(); for(int i=0; i<row; i++) { for(int j=0; j<=i; j++) { System.out.print(" "); } for(int j=0; j<row-i; j++) { 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 n = scanner.nextInt(); for (int i = 0; i <= n; i++) { for (int j = i; j < n ; j++) { System.out.print(" "); } for (int j = 0; j <= i ; j++) { System.out.print("* "); } System.out.println(); } for (int i = 1; i <= n ; i++) { for (int j = 1; j <= i ; j++) { System.out.print(" "); } for (int k = i; k <= n ; 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 a = sc.nextInt(); for(int i=1;i<=a+1;i++){ for(int j=0;j<a+1;j++){ if(j<a+1-i){ System.out.printf(" "); } else { System.out.printf("* "); } } System.out.println(); } for(int i=a;i>=1;i--){ for(int j=0;j<a;j++){ if(j==0){ System.out.printf(" "); } if(j<a-i){ System.out.printf(" "); } else { System.out.printf("* "); } } 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 = 0;n<=number;n++){ for(int s = number-n-1;s>=0;s--){ System.out.print(" "); } for(int i = n+1;i>0;i--){ System.out.print("* "); } System.out.println(""); } for(int n = 1;n <= number;n++){ for(int m = n;m>0;m--){ System.out.print(" "); } for(int j = number -n;j>=0;j--){ System.out.print("* "); } System.out.println(""); } } } }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner (System.in); int num = 0; while(scan.hasNextInt()){ num = scan.nextInt(); for(int i = 0; i < num ; i++){ for(int j = 0; j < num - i; j++){ System.out.print(" "); } for(int k = 0; k <= i; k++){ System.out.print("* "); } System.out.println(); } System.out.println(String.join("", Collections.nCopies(num + 1, "* "))); for(int i = 0; i < num ; i++){ for(int j = 0; j <= i; j++){ System.out.print(" "); } for(int k = 0; k < num - i; k++){ 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(); List<String> remember = new ArrayList<>(n); for (int i = 1; i <=n+1; i++) { String str = String.join("",Collections.nCopies(n+1-i," ")); str+=String.join("",Collections.nCopies(i,"* ")); remember.add(str); } boolean flag = true; for (int i = 0; i >=0 ;) { if (i==remember.size()-1)flag=false; System.out.println(remember.get(i)); i= flag?++i:--i; } } } }