拼出漂亮的表格

package cn.dlpu.lby;

import java.util.Scanner;

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

 要求考生把所有类写在一个文件中。调试好后,存入与考生文件夹下对应题号的“解答.txt”中即可。相关的工程文件不要拷入。请不要使用package语句。
 另外,源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。
 */
public class Pingezi {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		display(n, m);
	}

	private static void display(int n, int m) {
		// TODO Auto-generated method stub
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				if (i == 0) { // 第一行
					if (j == 0)
						System.out.print("┌─");
					else if (j > 0 && j < m) {
						System.out.print("┬─");
					} else {
						System.out.println("┐");
						add(j);
					}
				}

				// 中间行
				else if (i > 0 && i < n) {
					if (j == 0) {
						System.out.print("├─");
					} else if (j > 0 && j < m) {
						System.out.print("┼─");
					} else{
						System.out.println("┤");
						add(j);
					}
				}

				// 最后一行

				else{ 
					if (j == 0) {
					System.out.print("└─");
				}
					else if (j > 0 && j < m){
					System.out.print("┴─");
				} else
					System.out.print("┘");
				}

			}
		}
	}

	private static void add(int j) {
		// TODO Auto-generated method stub
		for (int i = 0; i < j; i++) {
			System.out.print("│ ");
		}
		System.out.println("│");
	}
}


原文地址:https://www.cnblogs.com/dyllove98/p/3148245.html