`
1140566087
  • 浏览: 547811 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
博客专栏
2c4ae07c-10c2-3bb0-a106-d91fe0a10f37
c/c++ 入门笔记
浏览量:18077
3161ba8d-c410-3ef9-871c-3e48524c5263
Android 学习笔记
浏览量:309556
Group-logo
J2ME 基础学习课程集
浏览量:18008
A98a97d4-eb03-3faf-af96-c7c28f709feb
Spring 学习过程记录...
浏览量:17196
社区版块
存档分类
最新评论

双层棱形

阅读更多
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;


public class 双层棱形 {
	public static void main(String[] args){
		// 层数:8层 ,列宽:16  行宽:16
		// * 号得总数为:层数*4-4	外层:8   则:内层  8-2
		int n = 16;
		int[][] arr = new int[2*n+1][2*n+1];
		int temp = 0;	//代表 * 的个数
		int x=1;
		int y=arr[x].length/2;

		// 起点为:中列 首行
		arr[x][y] = 1;	//1代表的是  * 号;

		//右下
		while(arr[x+1][y+1]==0 && y+1<arr[x+1].length-1){
			arr[++x][++y] = 1;
			++temp;
		}
		//左下
		while(arr[x+1][y-1]==0 && x+1<arr[x+1].length-1){
			arr[++x][--y] = 1;
			++temp;
		}
		//			//左上
		while(arr[x-1][y-1]==0 && y-1>0){
			arr[--x][--y] = 1;
			++temp;
		}
		//右上
		while(arr[x-1][y+1]==0 && x-1>=0){
			arr[--x][++y] = 1;
			++temp;
		}
		//			x=x+2;

		//---------------------------------------------内层----------------------------------------
		//右下
		arr[x+2][y] = 1;
		while(arr[x+1][y+1]==0 && y+1<arr[x+1].length-1-2){
			arr[++x][++y] = 1;
			++temp;
		}
		//左下
		while(arr[x+1][y-1]==0 && x+1<arr[x+1].length-1-2){
			arr[++x][--y] = 1;
			++temp;
		}
		//			//左上
		while(arr[x-1][y-1]==0 && y-1>2){
			arr[--x][--y] = 1;
			++temp;
		}
		//右上
		while(arr[x-1][y+1]==0 && x-1>=2){
			arr[--x][++y] = 1;
			++temp;
		}

		StringBuffer br = new StringBuffer();
		for(int i=1;i<arr.length-1;i++){
			String str = "";
			for(int j=1;j<arr[i].length-1;j++){
				str+=(arr[i][j]==1 ? "*":" ");
				System.out.print(arr[i][j]==1 ? "*" : " ");
			}
			bufferedWriter(str);
			str = "";
			System.out.println();
		}

	}
	//写入文件
	public static void bufferedWriter(String str){
		File f = new File("e:\\show.txt");
		//		String str = new String(s);
		try {
			if(!f.exists()){
				f.createNewFile();		
			}
			BufferedWriter br = new BufferedWriter(new FileWriter(f,true));
			br.write(str);
			br.write("\r\n");
			br.close();
		} catch (Exception  e) {	
			e.printStackTrace();
		}	


	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics