编程算法基础-3.1自顶向下风格

第三讲 风格与模式

3.1自顶向下风格

复杂问题分解,直到小问题足够简单,能够掌控为止

是一种思考方式

把大的任务不断的分解为更小的子任务

还有一法宝:忽略,忽略细节

程序问题

制定框架---》逐步细化---》逐步精华---》分解为子问题

打印特定的形状

做一个二维数组的缓冲区

向缓冲区输出

缓冲区输出屏幕上

数组初始元素都是0

表格:横线。竖线。

/*
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$                
$   $   $   $   $   $   $   $   $                
$   $   $   $   $   $   $   $   $                 
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$                
$   $   $   $   $   $   $   $   $                
$   $   $   $   $   $   $   $   $                
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$                
$   $   $   $   $   $   $   $   $                
$   $   $   $   $   $   $   $   $                
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*/                
package topToBottom;
 
public class Print {
    public static void main(String[] args) {
       char[][] a = new char[20][50];
       for(int i=0;i<4;i++){
           line_h(a, i*3, 0, 32);
       }
       for(int i=0;i<9;i++){
           line_v(a, i*4, 0, 9);
       }
       print(a);
    }
   
    //写入列
    public static void line_v(char  [][]a,int col,int row_start,int row_end){
       for(int i=row_start;i<=row_end;i++){
           a[i][col] = '$';
       }
    }
   
    //写入行
    public static void line_h(char  [][]a,int row,int col_start,int col_end){
       for(int i=col_start;i<=col_end;i++){
           a[row][i] = '$';
       }
    }
   
    //打印
    public static void print(char[][] a){
       for(int i=0;i<a.length;i++){
           for(int j=0;j<a[i].length;j++){
              if(a[i][j]==0){
                  System.out.print(" ");
              }else{
                  System.out.print(a[i][j]);
              }
           }
           System.out.println();
       }
    }
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$                
$   $   $   $   $   $   $   $   $                
$   $   $   $   $   $   $   $   $                
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$                
$   $   $   $   $   $   $   $   $                
$   $   $   $   $   $   $   $   $                
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$                
$   $   $   $   $   $   $   $   $                
$   $   $   $   $   $   $   $   $                
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$                

Word排版问题,没有对齐

设计程序-打印

设计程序-打印
/*
设计程序
在中文Windows环境下,控制台窗体中也能够用特殊符号拼出美丽的表格来。
比方:
┌─┬─┐
│ │ │
├─┼─┤
│ │ │
└─┴─┘
事实上,它是由例如以下的符号拼接的:
左上 = ┌
上 = ┬
右上 = ┐
左 = ├
中心 = ┼
右 = ┤
左下= └
下 = ┴
右下 = ┘
垂直 = │
水平 = ─
本题目要求编写一个程序,依据用户输入的行、列数画出对应的表格来。
比如用户输入:
3 2
则程序输出:
┌─┬─┐
│ │ │
├─┼─┤
│ │ │
├─┼─┤
│ │ │
└─┴─┘
用户输入:
2 3
则程序输出:
┌─┬─┬─┐
│ │ │ │
├─┼─┼─┤
│ │ │ │
└─┴─┴─┘
*/

package topToBottom;

public class Code {
	public static void main(String[] args) {
		int a = 2;//行数
		int b = 3;//列数
		createArray(a, b);
	}
	//创建数组
	public static void createArray(int a,int b){
		char[][] c = new char[2*a+1][2*b+1];
		c[0][0] = '┌';//左上
		c[0][2*b+1-1] = '┐';//右上
		c[2*a+1-1][0] = '└';//左下
		c[2*a+1-1][2*b+1-1] = '┘';//右下
		
		firstAndLastRow(c, a, b);
		singleRow(c, a, b);
		doubleRow(c, a, b);
		singleColumn(c, a, b);
		printGrid(c,a,b);
	}
	//第奇数列
	public static char[][] singleColumn(char[][] c,int a,int b){
		for(int i=0;i<2*a+1;i+=2){
			for(int j=1;j<2*b;j+=2){
				c[i][j]='─';
			}
		}
		return c;
	}
	//第偶数行
	public static char[][] doubleRow(char[][]c,int a,int b){
		for(int i=2;i<2*a-1;i+=2){
			c[i][0] = '├';
			c[i][2*b] = '┤';
			for(int j=2;j<2*b;j+=2){
				c[i][j] = '┼';
			}
		}
		return c;
	}
	
	//第奇数行
	public static char[][] singleRow(char[][]c,int a,int b){
		for(int i=1;i<2*a;i+=2){
			for(int j=0;j<2*b+1;j+=2){
				c[i][j]='│';
			}
		}
		return c;
	}
	
	//第一行和最后一行
	public static char[][] firstAndLastRow(char[][]c,int a,int b){
		for(int i=2;i<2*b+1-1;i+=2){//第一行和最后一行
			c[0][i] = '┬';
			c[2*a+1-1][i] = '┴';
		}
		return c;
	}
	//打印表格
	public static void printGrid(char[][] c,int a,int b){
		for(int i=0;i<2*a+1;i++){
			for(int j=0;j<2*b+1;j++){
				System.out.print(c[i][j]);
			}
			System.out.println();
		}
	}
}

┌─┬─┬─┐
│ │ │ │
├─┼─┼─┤
│ │ │ │
└─┴─┴─┘
原文地址:https://www.cnblogs.com/cynchanpin/p/6708508.html