JAVA打印棱形
作者:互联网
闲来无事做做题,有可以改进的地方还希望可以指正!
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = 0;
while (true) {
if (num % 2 == 0) {
System.out.print("请输入单数:");
num = scanner.nextInt();
} else {
break;
}
}
// 中间值
int middle = (num + 1) / 2;
for (int i = 1; i <= num; i++) {
// 行绝对值
int row = middle - Math.abs(i - middle);
for (int j = 1; j <= num; j++) {
// 列绝对值
int column = middle - Math.abs(j - middle);
if (middle - row >= column) {
System.out.printf(" ");
} else {
System.out.printf("*");
}
}
System.out.println();
}
}
输出结果:
标签:JAVA,scanner,int,打印,System,num,棱形,printf,out 来源: https://blog.csdn.net/qq_33666686/article/details/103971527