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

来源:互联网 发布:兄弟连php课程大纲 编辑:程序博客网 时间:2024/06/07 01:43

自顶向下风格:    1.把大的任务不断的分解成更小的任务

                                        2.忽略细节,把握整体的框架

 

程序问题-------->制定框架--------->逐步细化---------->逐步精化

 

打印:

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 

$      $      $      $      $      $      $      $      $ 

$      $      $      $      $      $      $      $      $ 

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     

$      $      $      $      $      $      $      $      $ 

$      $      $      $      $      $      $      $      $ 

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     

$      $      $      $      $      $      $      $      $ 

$      $      $      $      $      $      $      $      $ 

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$                    

package NO3;public class test01 {private static void print(char[][] ch){for(int i = 0; i < ch.length; i++){for(int j = 0; j < ch[i].length;j++){if(ch[i][j] == 0)System.out.print(" ");else System.out.print(ch[i][j]);}System.out.println();}}public static void main(String[] args) {// TODO Auto-generated method stub       char[][] cache = new char[20][50];                            for(int i = 0; i < 4;i++)          line_h(cache,i * 3,0,33);       for(int i = 0; i < 9;i++)          line_l(cache, i * 4 ,0,10);       //cache[1][1] = '$';       //cache[1][2] = '$';print(cache);}private static void line_l(char[][] cache, int row,int line1, int line2) {// TODO Auto-generated method stubfor(int i = line1; i < line2;i++)cache[i][row] = '$';}private static void line_h(char[][] cache, int line, int row1 , int row2) {// TODO Auto-generated method stubfor(int i = row1 ;i < row2; i++){    cache[line][i] = '$';}}}

 



 

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

比如:

┌─┬─┐

│   │  │

├─┼─┤

│   │  │

└─┴─┘

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

左上 = ┌

上 = ┬

右上 = ┐

左 = ├

中心 = ┼

右 = ┤

左下= └

下 = ┴

右下 = ┘

垂直 = │

水平 = ─

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

例如用户输入:

3 2

则程序输出:

┌─┬─┐

│   │  │

├─┼─┤

│   │  │

├─┼─┤

│   │  │

└─┴─┘

用户输入:

2 3

则程序输出:

┌─┬─┬─┐

│   │  │   │

├─┼─┼─┤

│   │  │   │

└─┴─┴─┘

 

package NO3;import java.util.Scanner;public class Test03 {public static void main(String[] args) {// TODO Auto-generated method stub       String[][]  ss = new String[20][50];                     Scanner input = new Scanner(System.in);       int line = input.nextInt();       int row  = input.nextInt();       line_top(ss,0,0,row + 1);       line_bottom(ss, line * 2, 0, row + 1);       for(int i = 1; i < line; i++)           line_center(ss, i * 2 , 0 ,row +1);       for(int i = 0; i < line; i++)           line_else(ss,i * 2 + 1 ,0,  row + 1);       print(ss);}private static void line_else(String[][] ss, int line, int row1, int row2) {// TODO Auto-generated method stubfor(int i = row1; i < row2; i++){ss[line][i] = "│";}}private static void line_center(String[][] ss, int line, int row1, int row2) {// TODO Auto-generated method stubfor(int i = row1; i < row2; i++){ if(i == row1) ss[line][i] = "├"; else if(i == row2 - 1) ss[line][i] = "┤" ; else  ss[line][i] = "┼";}}private static void line_bottom(String[][] ss, int line, int row1, int row2) {// TODO Auto-generated method stubfor(int i = row1; i < row2; i++){    if(i == row1)    ss[line][i] = "└";    else if(i == row2 - 1)    ss[line][i] = "┘";    else     ss[line][i] = "┴";}}private static void line_top(String[][] ss, int line, int row1, int row2) {// TODO Auto-generated method stubfor(int i = row1; i < row2; i++){    if(i == row1)    ss[line][i] = "┌";    else if(i == row2 - 1)    ss[line][i] = "┐";    else     ss[line][i] = "┬";}}private static void print(String[][] ss) {// TODO Auto-generated method stubfor(int i = 0; i < ss.length; i++){for(int j = 0; j < ss[i].length; j++){    if(ss[i][j] == null)    System.out.print(" ");    else     System.out.print(ss[i][j]);   }System.out.println();}}}


 

 

 

 

0 0