标签:class color 开始 int 部分 解析 技术 log 字符
小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:
对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 5 public class Main { 6 private static String[][] str; 7 public static void main(String[] args) throws IOException{ 8 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 int n = Integer.parseInt(br.readLine()); 10 11 print(n); 12 13 } 14 15 //打印十字图 16 private static void print(int n) { 17 int row = 9 + 4 * ( n - 1 ); //计算出行数 18 str = new String[row][row]; 19 for(int i = 0; i < row; i++) { //先在所有单元格中存入 "." 20 for(int j = 0; j < row; j++) { 21 str[i][j] = "."; 22 } 23 } 24 25 int center = row / 2; //找出中心 26 for(int i = center - 2; i <= center + 2; i++){ //打印中心十字(图中背景颜色为蓝色) 27 str[center][i] = "$"; 28 str[i][center] = "$"; 29 } 30 31 32 for(int layer = 0; layer < n; layer++) { //layer:控制打印的圈数 33 //打印四周图形(图中背景颜色为橙色),坐标范围为中心偏左两个单元格开始,中心偏右两个单元格结束,增加一圈即往外增加两个单元格 34 for(int x = (center - 2 - 2*layer); x <= (center + 2 + 2*layer); x++) { 35 str[center - 4 - 2*layer][x] = "$"; 36 str[x][center - 4 - 2*layer] = "$"; 37 str[center + 4 + 2*layer][x] = "$"; 38 str[x][center + 4 + 2*layer] = "$"; 39 } 40 //打印图中背景颜色为绿色和紫色的图形,坐标范围为中心偏左三个单元格开始,中心偏右三个单元格结束,增加一圈即往外增加两个单元格 41 for(int y = center - 3 - 2*layer; y <= center + 3 +2*layer; y++){ 42 str[center - 2 - 2*layer][y] = "$"; 43 str[y][center - 2 - 2*layer] = "$"; 44 str[center + 2 + 2*layer][y] = "$"; 45 str[y][center + 2 + 2*layer] = "$"; 46 } 47 //将背景颜色为紫色的单元格变为"." 48 str[center - 2 - 2*layer][center - 1 - 2*layer] = "."; 49 str[center - 1 - 2*layer][center - 2 - 2*layer] = "."; 50 str[center - 2 - 2*layer][center + 1 + 2*layer] = "."; 51 str[center - 1 - 2*layer][center + 2 + 2*layer] = "."; 52 str[center + 2 + 2*layer][center - 1 - 2*layer] = "."; 53 str[center + 1 + 2*layer][center - 2 - 2*layer] = "."; 54 str[center + 2 + 2*layer][center + 1 + 2*layer] = "."; 55 str[center + 1 + 2*layer][center + 2 + 2*layer] = "."; 56 } 57 58 //输出图形 59 for(int i = 0; i < row; i++) { 60 for(int j = 0; j < row; j++) { 61 System.out.print(str[i][j]); 62 } 63 System.out.printf("\n"); 64 } 65 66 } 67 }
标签:class color 开始 int 部分 解析 技术 log 字符
原文地址:http://www.cnblogs.com/cao-lei/p/6637202.html