java 简单表格打印

来源:互联网 发布:有域名怎么建站 编辑:程序博客网 时间:2024/06/05 16:12


在中文Windows环境下,控制台窗口中也可以用特殊符号拼出漂亮的表格来。
比如:
┌─┬─┐
│  │  │
├─┼─┤
│  │  │
└─┴─┘
其实,它是由如下的符号拼接的:
左上 = ┌
上 =  ┬
右上 =  ┐
左 =  ├
中心 =  ┼
右 =  ┤
左下=  └
下 =  ┴
右下 =  ┘
垂直 =  │
水平 =   ─
本题目要求编写一个程序,根据用户输入的行、列数画出相应的表格来。


例如用户输入:
3 2
则程序输出:
┌─┬─┐
│  │  │
├─┼─┤
│  │  │
├─┼─┤
│  │  │

└─┴─┘

import java.util.Arrays;import java.util.Scanner;public class 制作表格 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("输入表格的行列数。如:3 2");int row = sc.nextInt();int colum = sc.nextInt();int r = 3 + (row - 1) * 2;int c = 3 + (colum - 1) * 2;char[][] table = new char[r][c];for (int i = 0; i < table.length; i++) {Arrays.fill(table[i], ' ');}table[0][0] = '┌';table[0][c - 1] = '┐';table[r - 1][0] = '└';table[r - 1][c - 1] = '┘';char t = '─';boolean change = true;for (int i = 1; i < table[0].length - 1; i++) {table[0][i] = t;if (change) {t = '┬';change = false;continue;}t = '─';change = true;}change = true;t = '─';for (int i = 1; i < table[0].length - 1; i++) {table[r - 1][i] = t;if (change) {t = '┴';change = false;continue;}t = '─';change = true;}for (int i = 1; i < table.length - 1; i += 2) {for (int j = 0; j < table[i].length; j += 2) {table[i][j] = '│';}}for (int i = 2; i < table.length - 1; i += 2) {table[i][0] = '├';table[i][c - 1] = '┤';t = '─';change = true;for (int j = 1; j < table[i].length - 1; j++) {table[i][j] = t;if (change) {t = '┼';change = false;continue;}t = '─';change = true;}}for (int i = 0; i < table.length; i++) {for (int j = 0; j < table[i].length; j++) {System.out.print(table[i][j]);}System.out.println();}}}



原创粉丝点击