标题: 分享一个打印菱形的小代码 [打印本页] 作者: 青菜市场 时间: 2015-12-22 18:35 标题: 分享一个打印菱形的小代码 import java.util.Scanner;
class LingXing {
public static void lingXing(int h) {
int z = h/2;
for (int y = 1;y<=h;y++ ){
if (y<z+1){
for (int x = z;x>=y ;x-- ){
System.out.print(" ");
}
for (int x= 1;x<=y ;x++ ){
System.out.print("* ");
}
System.out.println();
}else if (y>z+1){
for (int x = z+2 ;x<=y ;x++ ){
System.out.print(" ");
}
for (int x= h;x>=y ;x-- ){
System.out.print("* ");
}
System.out.println();
}else {
for (int i=1;i<=z+1;i++ ){
System.out.print("* ");
}
System.out.println();
}
}
}
}
class LingXing_Test{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("输入一个奇数给你打印相应行数的菱形");
int x = sc.nextInt();
if (x%2==1){
LingXing.lingXing(x);
}else {
System.out.println("输入的数字必须为奇数");
}
}
}
欢迎大家多指点