题目:打印出如下图案(菱形)
*
***
******
********
******
***
*
编程工具使用eclipse
代码如下:
package test; public class starsList { public int totaLines = 0; // 最大行数 public static void main(String [] args){ starsList aaa = new starsList(); aaa.setNum(17,17);// 第一个是要显示的行数。第二个是总行数 } // 单行星号打印(空格和星号) public void prints(int num,int totals){ String string = ""; int lang = totals-num>=0 ? num : num-(num-totals)*2; for(int a=0;a<lang;a++){ string +="*"; } int space = (totals-lang)/2; for(int b=0;b<space;b++){ string = " "+string; string = string+" "; } System.out.println(string); System.out.println(' '); } // 根据打印行数计算每行星号个数并调用单行打印 public void setNum(int lines,int totals2){ if(lines>totals2){ System.out.println("要显示的行数("+lines+") 大于总行数("+totals2+")"); return; } starsList bb = new starsList(); for(int c = 0;c<lines;c++){ int n = (2*c)+1; bb.prints(n,totals2); } } }