标题: 一道题打印棱形 [打印本页] 作者: 革命卍 时间: 2016-6-3 22:41 标题: 一道题打印棱形 棱形图形棱形图形作者: D-Carl 时间: 2016-6-3 22:44
刚刚不是有个提问正三角形的吗? 和那个类似
import java.util.Scanner;
class Demo {
public static void main(String[] args) {
System.out.println(" 请输入一个正奇整数:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a >= 0 && a%2 == 1) {
for (int i = 1; i <= a; i++) {
int y = java.lang.Math.abs((a + 1)/2 - i);
for (int b=1; b <= y; b++) {
System.out.print(" ");
}
for (int c=1; c <= (a - 2*y); c++) {
System.out.print("*");
}
for (int d=1; d <= y; d++) {
System.out.print(" ");
}
System.out.println();