打印菱形*
/**
* 8、在控制台上打印一个菱形:
*
***
*****
*******
*****
***
*
*/
package com.itheima;
public class Test8 {
public static void main(String[] args) {
int num=7;
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num - i; j++) {
//System.out.print(" "); //这样瘦长一点
System.out.print(" ");
}
for (int j = 1; j <= 2 * (i + 1) - 3; j++) {
System.out.print("* ");
}
System.out.println();
}
// 打印下半部分图象
for (int i = 1; i <= num - 1; i++) {
for (int j = 1; j <= i; j++) {
//System.out.print(" "); //这样瘦长一点
System.out.print(" ");
}
int k = num - 1 - i;
for (int j = 1; j <= 2 * k + 1; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
————————————————————————————
package cn.mldn.demo;
public class TestDemo{
public static void main(String[] args) throws Exception {
int num=7;
for(int i=0;i<num;i+=2){
for(int k=i;k<num;k+=2){
System.out.print(" ");
}
for(int j=num-i+1;j<num;j++){
System.out.print("*");
}
System.out.println();
}
for(int i=num;i>0;i-=2){
for(int j=num-i;j>0;j-=2){
System.out.print(" ");
}
for(int k=i;k>0;k--){
System.out.print("*");
}
System.out.println();
}
}
}