拼出漂亮的表格

来源:互联网 发布:好用的祛斑精华 知乎 编辑:程序博客网 时间:2024/05/01 16:10
 

拼出漂亮的表格

分类: 蓝桥杯软件大赛编程题 151人阅读 评论(0) 收藏 举报
[java] view plaincopy
  1. package cn.dlpu.lby;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. /*拼出漂亮的表格 
  6.  * 在中文Windows环境下,控制台窗口中也可以用特殊符号拼出漂亮的表格来。 
  7.  比如:         
  8.  ┌─┬─┐ 
  9.  │ │ │ 
  10.  ├─┼─┤ 
  11.  │ │ │ 
  12.  └─┴─┘       
  13.  其实,它是由如下的符号拼接的: 
  14.  左上 = ┌ 
  15.  上 =  ┬ 
  16.  右上 =  ┐ 
  17.  左 =  ├ 
  18.  中心 =  ┼ 
  19.  右 =  ┤ 
  20.  左下=  └ 
  21.  下 =  ┴ 
  22.  右下 =  ┘ 
  23.  垂直 =  │ 
  24.  水平 =   ─ 
  25.  本题目要求编写一个程序,根据用户输入的行、列数画出相应的表格来。 
  26.  例如用户输入: 
  27.  3 2 
  28.  则程序输出: 
  29.  ┌─┬─┐ 
  30.  │ │ │ 
  31.  ├─┼─┤ 
  32.  │ │ │ 
  33.  ├─┼─┤ 
  34.  │ │ │ 
  35.  └─┴─┘ 
  36.  用户输入: 
  37.  2 3 
  38.  则程序输出: 
  39.  ┌─┬─┬─┐ 
  40.  │ │ │ │ 
  41.  ├─┼─┼─┤ 
  42.  │ │ │ │ 
  43.  └─┴─┴─┘ 
  44.  
  45.  要求考生把所有类写在一个文件中。调试好后,存入与考生文件夹下对应题号的“解答.txt”中即可。相关的工程文件不要拷入。请不要使用package语句。 
  46.  另外,源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。 
  47.  */  
  48. public class Pingezi {  
  49.     public static void main(String[] args) {  
  50.         Scanner sc = new Scanner(System.in);  
  51.         int n = sc.nextInt();  
  52.         int m = sc.nextInt();  
  53.         display(n, m);  
  54.     }  
  55.   
  56.     private static void display(int n, int m) {  
  57.         // TODO Auto-generated method stub  
  58.         for (int i = 0; i <= n; i++) {  
  59.             for (int j = 0; j <= m; j++) {  
  60.                 if (i == 0) { // 第一行  
  61.                     if (j == 0)  
  62.                         System.out.print("┌─");  
  63.                     else if (j > 0 && j < m) {  
  64.                         System.out.print("┬─");  
  65.                     } else {  
  66.                         System.out.println("┐");  
  67.                         add(j);  
  68.                     }  
  69.                 }  
  70.   
  71.                 // 中间行  
  72.                 else if (i > 0 && i < n) {  
  73.                     if (j == 0) {  
  74.                         System.out.print("├─");  
  75.                     } else if (j > 0 && j < m) {  
  76.                         System.out.print("┼─");  
  77.                     } else{  
  78.                         System.out.println("┤");  
  79.                         add(j);  
  80.                     }  
  81.                 }  
  82.   
  83.                 // 最后一行  
  84.   
  85.                 else{   
  86.                     if (j == 0) {  
  87.                     System.out.print("└─");  
  88.                 }  
  89.                     else if (j > 0 && j < m){  
  90.                     System.out.print("┴─");  
  91.                 } else  
  92.                     System.out.print("┘");  
  93.                 }  
  94.   
  95.             }  
  96.         }  
  97.     }  
  98.   
  99.     private static void add(int j) {  
  100.         // TODO Auto-generated method stub  
  101.         for (int i = 0; i < j; i++) {  
  102.             System.out.print("│ ");  
  103.         }  
  104.         System.out.println("│");  
  105.     }  
  106. }  
原创粉丝点击