java输入树的高度打印圣诞树 我的代码如下 但打印出来的*都不在同一行
该怎么修改? 别去复制粘贴 展开
2016-11-21
println是打印后换行,print则是直接打印
package com.lg.Demo_1;
import java.util.Scanner;
public class Demo_10 {
public static void main(String[] args){
System.out.println("请输入树的高度:");
Scanner sc =new Scanner(System.in);
int a =sc.nextInt();
for (int i = 1; i<=a; i++) {
for (int j2 = a; j2 >=i; j2--) {
System.out.print(" ");
}
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
for (int j1 = 1; j1 <i; j1++) {
System.out.print("*");
}
System.out.println("");
}
if (a%2==0) {
for (int y = 1; y <=(a/2); y++) {
for (float x =a/2; x <=a; x++) {
System.out.print(" ");
}
System.out.print("|");
for (int i1 = 3; i1 <=a; i1++) {
System.out.print("|");
}System.out.print("\n");
}
}else{
for (int y = 1; y <=(a/2); y++) {
for (float x =a/2; x <=a; x++) {
System.out.print(" ");
}
System.out.print("|");
for (int i1 = 3; i1 <a; i1++) {
System.out.print("|");
}
System.out.print("\n");
}
}
}
}
新鲜出炉,无需解释,看图