03-01自顶向下风格_编程

设计程序

在中文Windows环境下,控制台窗口中也可以用特殊符号拼出漂亮的表格来。

比如:

┌─┬─┐

│   │  │

├─┼─┤

│   │  │

└─┴─┘

其实,它是由如下的符号拼接的:

左上 = ┌

上 = ┬

右上 = ┐

左 = ├

中心 = ┼

右 = ┤

左下= └

下 = ┴

右下 = ┘

垂直 = │

水平 = ─

本题目要求编写一个程序,根据用户输入的行、列数画出相应的表格来。

例如用户输入:

3 2

则程序输出:

┌─┬─┐

│   │  │

├─┼─┤

│   │  │

├─┼─┤

│   │  │

└─┴─┘

用户输入:

2 3

则程序输出:

┌─┬─┬─┐

│   │  │   │

├─┼─┼─┤

│   │  │   │

└─┴─┴─┘

标准答案:

class MyCell
{
	private char leftUp = '┌';
	private char up = '┬';
	private char rightUp = '┐';
	private char left = '├';
	private char center = '┼';
	private char right = '┤';
	private char leftDown = '└';
	private char down = '┴';
	private char rightDown = '┘';
	private char ver = '│';
	private char hor = '─';
	
	private int row = 2;  // 行数
	private int col = 2;  // 列数
	
	public void setRow(int x)
	{
		if(row>=1 && row <=20) row = x;
	}
	
	public void setCol(int x)
	{
		if(col>=1 && col <=10) col = x;
	}
	
	public void show()
	{
		printBeginRow();  //首行特殊
		for(int i=0; i<row-1; i++)
		{
			printRow1();  // 空格+竖线
			printRow2();  // 横线+转角
		}
		printRow1();
		printEndRow();  // 末行特殊
	}
	
	private void printBeginRow()
	{
		System.out.print(leftUp);
		for(int i=0; i<col-1; i++)
		{
			System.out.print(hor);
			System.out.print(up);
		}
		System.out.print(hor);
		System.out.print(rightUp);
		System.out.println();
	}
	
	private void printEndRow()
	{
		System.out.print(leftDown);
		for(int i=0; i<col-1; i++)
		{
			System.out.print(hor);
			System.out.print(down);
		}
		System.out.print(hor);
		System.out.print(rightDown);
		System.out.println();
	}
	
	private void printRow1()
	{
		System.out.print(ver);
		for(int i=0; i<col; i++)
		{
			System.out.print("  ");
			System.out.print(ver);
		}
		System.out.println();
	}
	
	private void printRow2()
	{
		System.out.print(left);
		for(int i=0; i<col-1; i++)
		{
			System.out.print(hor);
			System.out.print(center);
		}
		System.out.print(hor);
		System.out.print(right);
		System.out.println();
	}
	
	
}	
	
public class PinBiaoGe
{
	public static void main(String[] args)
	{
		// 在字符界面用特殊符号拼图形
		/*		
		┌─┬─┐
		│  │  │
		├─┼─┤
		│  │  │
		└─┴─┘		
		*/
		
		MyCell a = new MyCell();
		a.setRow(1);
		a.setCol(1);		
		a.show();
		
		a.setRow(1);
		a.setCol(4);		
		a.show();
		
		a.setRow(4);
		a.setCol(1);		
		a.show();
		
		a.setRow(5);
		a.setCol(5);		
		a.show();
		
		a.setRow(10);
		a.setCol(18);		
		a.show();
		
		
		// 如何设置行距离和列距离
		
	
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值