JAVA两个关于数组输出图形的编程题。
发布网友
发布时间:2023-07-16 05:55
我来回答
共1个回答
热心网友
时间:2024-12-10 16:35
2.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n, i, j, k;
char[][] arr;
//控制必须键入奇数
do {
n = scan.nextInt();
}while(n%2 == 0);
arr = new char[n][n];
for(i=0; i<(n+1)/2; i++) {
for(j=0; j<n/2-i; j++) {
arr[i][j] = ' ';
}
for(k=0; k<2*i+1; k++) {
arr[i][j+k] = '*';
}
}
for(i=0; i<n/2; i++) {
for(j=0; j<i+1; j++) {
arr[(n+1)/2+i][j] = ' ';
}
for(k=0; k<n-2-2*i; k++) {
arr[(n+1)/2+i][j+k] = '*';
}
}
for(i=0; i<n; i++) {
for(j=0; j<n; j++){
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}
3.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n, i, j;
char[][] arr;
n = scan.nextInt();
arr = new char[n][];
for(i=0; i<n; i++) {
arr[i] = new char[i+1];
for(j=0; j<=i; j++) {
arr[i][j] = '*';
}
}
for(i=0; i<n; i++) {
for(j=0; j<=i; j++){
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}