Java学习笔记(一)

来源:互联网 发布:百度云天空八部源码 编辑:程序博客网 时间:2024/06/05 19:56

Java实现打印镂空图形

源代码:

public class Demo3 {public static void main(String[] args) {int lay = 4;//表示有多少层for(int i = 1; i <= lay; i ++) {//找出空格规律//1->3 2->2 3->1 4->0for(int k = 1; k <= lay - i; k++) {System.out.print(" ");}//打印*//1->1 2->3 3->5 4->7for(int j = 1; j <= 1 + (i - 1)*2; j++) {//首行和莫行正常打印*if(i == 1 || i == lay) {System.out.print("*");} else {//该行第一个和最后一个打印*if(j == 1 || j == 1 + (i - 1) * 2) {System.out.print("*");} else {System.out.print(" ");}}}//打出一个换行System.out.println();}}}

运行结果:



0 0
原创粉丝点击