该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
通用的方法,可以输入打印的数量:
/**
* 打印星星的实例,要求用户输入一个整型数,
* 程序打印出菱形图形。
*
* @author Dong Wang
* @since 2006年3月30日
* @link http://www.4ugo.com
*
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AsteriskPrint {
private int _num_space[]; //空格存储对象
private int _num_star[]; //星星存储对象
private int _dimension; //最大星星数量
public AsteriskPrint(int dimension){
this._dimension = dimension;
this.setValue();
}
public int get_dimension(){
return this._dimension;
}
public int[] get_num_space(){
return this._num_space;
}
private void setValue(){
int i= this.get_dimension()/2;
_num_space = new int[i+1];
_num_star = new int[i+1];
while(i>=0){
//计算空格数量
_num_space[i]=i%this.get_dimension();
//计算星星数量
_num_star[i] = this.get_dimension() - (_num_space[i]*2);
i--;
}
}
public void printStar(){
int counter = (this.get_dimension()/2)+1;
for(int i=0;i
if(i>this.get_dimension()/2){
counter++;
}else{
counter--;
}
for (int j = 0; j
System.out.print(" ");
}
//打印星星
for (int k = 0; k
System.out.print("*");
}
System.out.println();
}
}
public static void main(String argus[]) throws IOException {
System.out.print("Enter a number (>0) :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine().trim());
AsteriskPrint ap = new AsteriskPrint(num);
ap.printStar();
System.exit(0);
}
}