标签:控制 方案 class void 分享 href blog 技术 head
目录
小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:
对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。
具体代码如下:
import java.util.Scanner; public class Main { public void getResult(int n) { int len = 5 + 4 * n; int mid = len / 2; char[][] result = new char[len][len]; for(int i = 0;i < len;i++) for(int j = 0;j < len;j++) result[i][j] = ‘.‘; for(int i = mid - 2;i <= mid + 2;i++) { result[mid][i] = ‘$‘; result[i][mid] = ‘$‘; } for(int i = 1;i <= n;i++) { for(int j = mid - 2 * i;j <= mid + 2 * i;j++) { result[mid - 2 * (i + 1)][j] = ‘$‘; result[mid + 2 * (i + 1)][j] = ‘$‘; result[j][mid - 2 * (i + 1)] = ‘$‘; result[j][mid + 2 * (i + 1)] = ‘$‘; } //左上角 result[mid - 2 * i][mid - 2 * i] = ‘$‘; result[mid - 2 * i][mid - 2 * i - 1] = ‘$‘; result[mid - 2 * i - 1][mid - 2 * i] = ‘$‘; //右上角 result[mid - 2 * i][mid + 2 * i] = ‘$‘; result[mid - 2 * i - 1][mid + 2 * i] = ‘$‘; result[mid - 2 * i][mid + 2 * i + 1] = ‘$‘; //左下角 result[mid + 2 * i][mid - 2 * i] = ‘$‘; result[mid + 2 * i][mid - 2 * i - 1] = ‘$‘; result[mid + 2 * i + 1][mid - 2 * i] = ‘$‘; //右下角 result[mid + 2 * i][mid + 2 * i] = ‘$‘; result[mid + 2 * i][mid + 2 * i + 1] = ‘$‘; result[mid + 2 * i + 1][mid + 2 * i] = ‘$‘; } for(int i = 0;i < len;i++) { for(int j = 0;j < len;j++) { System.out.print(result[i][j]); } System.out.println(); } } public static void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(System.in); int n = in.nextInt(); test.getResult(n); } }
标签:控制 方案 class void 分享 href blog 技术 head
原文地址:http://www.cnblogs.com/liuzhen1995/p/6833059.html