多组输入,一个整数(3~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 == 0 || j == n - 1 || i == 0 || i == n - 1) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } } in.close(); } }
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=1;j<=n;j++){ if(i==1||i==n||j==1||j==n){ System.out.print("* "); }else{ 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.hasNextInt()){ int x=sc.nextInt(); for(int i=1;i<=x;i++){ for(int j=1;j<=x;j++){ if(i==1||i==x){ System.out.printf("* "); }else{ if(j==1||j==x) 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.hasNextInt()) { int n = sc.nextInt(); //第一行 for (int i = 0; i < n; i++) { System.out.print("* "); } System.out.println(); //中间行 for (int j = 0; j < n - 2; j++) { System.out.print("* "); for (int i = 0; i < n - 2; i++) { System.out.print(" "); } System.out.println("* "); } //最后一行 for (int i = 0; i < n; i++) { 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 = 1 ; i <= n ;i++){ if(i != 1 && i != n){ System.out.print("* "); for(int j = 1;j <= n - 2;j++){ System.out.print(" "); } System.out.print("* "); }else{ for(int j = 1; j <= n;j++){ 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;i++) { for(int j=1;j<=a;j++) { if(i==1||j==1||i==a||j==a) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } } } }
import java.util.Scanner; /** * @Title: 空心正方形图案 * @Remark: KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的“空心”正方形图案。 * 输入描述: * 多组输入,一个整数(3~20),表示输出的行数,也表示组成正方形边的“*”的数量。 * 输出描述: * 针对每行输入,输出用“*”组成的“空心”正方形,每个“*”后面有一个空格。 * @Author: ijunfu * @Version: 1.0.0 * @Date: 2022-03-20 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNextLine()) { int count = Integer.parseInt(in.nextLine()); for(int i=0; i<count; i++) { for(int j=0; j<count; j++) { if(i == 0 || j == 0 || i == count - 1 || j == count -1) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } } } }
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++){ int m = 2*n; char[] ch = new char[m]; //初始化。在牛客需要,eclipse上不用初始化。是jdk版本问题吗?没搜到原因 //求大佬解答 for(int k = 0; k < m; k++){ ch[k] = ' '; } ch[0] = '*'; ch[m-2] = '*'; if(i == 0 || i == n-1){ for(int j = 0 ;j < m; j++){ if(j%2 == 0){ ch[j] = '*'; }else{ ch[j] = ' '; } } } System.out.println(String.valueOf(ch)); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int k=sc.nextInt(); for(int i=0;i<k;i++){ for (int j=0;j<k;j++){ if(i==0||i==k-1) { System.out.print("* "); } else{ if (j==0||j==k-1){ System.out.print("* "); } else{ 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(); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(i==0||i==(n-1)||j==0||j==(n-1)){ System.out.print("* "); }else{ 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[][] arr = new String[n][n]; for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ arr[i][j] = " "; arr[0][j] = "* "; arr[n-1][j] = "* "; arr[i][0] = "* "; arr[i][n-1] = "* "; } } for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ System.out.print(arr[i][j]); } System.out.println(); } } } }
import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); while(scan.hasNext()){ int n=scan.nextInt(); String res[][]=new String[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ res[i][j]=" "; } } for(int i=0;i<n;i++){ res[0][i]="*"; res[n-1][i]="*"; } for(int i=1;i<n-1;i++){ res[i][0]="*"; res[i][n-1]="*"; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ System.out.print(res[i][j]+" "); } System.out.println(); } } } }最后那个空格比较难发现
import java.util.Collections; import java.util.Scanner; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); if (n==1){ System.out.println("* "); continue; } //以下代码为str 为重复n次"* "的字符串。 String str = Collections.nCopies(n,"* ").stream().collect(Collectors.joining()); String temp = Collections.nCopies((n-2)*2," ").stream().collect(Collectors.joining()); temp = "* "+ temp + "* "; System.out.println(str); for (int i = 0; i < n - 2; i++) { System.out.println(temp); } System.out.println(str); } } }