黑马程序员技术交流社区

标题: 打印图形代码的问题 [打印本页]

作者: 王睿    时间: 2012-3-9 00:32
标题: 打印图形代码的问题
  1. public class demo {

  2.         public static void main(String[] args) {
  3.                 int num = 23;
  4.                 int height = num/4+1;
  5.                 int width = num;
  6.                
  7.                 int[][] arr = new int[height][width];
  8.                 int x = height-1;
  9.                 int y = 0;
  10.                 boolean count = true;
  11.                 for(int i=1; i<=width; i++){
  12.                         arr[x][y] = i;
  13.                         y++;
  14.                         if(count){
  15.                                 x--;
  16.                         }else{
  17.                                 x++;
  18.                         }
  19.                         if(x<0){
  20.                                 x=x+2;
  21.                                 count = false;
  22.                         }
  23.                         if(x>height-1){
  24.                                 x=x-2;
  25.                                 count = true;
  26.                         }               
  27.                 }
  28.                
  29.                 for(int i=0; i<arr.length; i++){
  30.                         for(int j=0; j<arr[i].length; j++){
  31.                                 if(arr[i][j]==0){
  32.                                         System.out.print(" ");
  33.                                 }else{
  34.                                         System.out.print(arr[i][j]);
  35.                                 }
  36.                         }
  37.                         System.out.println();
  38.                 }
  39.         }
  40.        
  41. }
复制代码
打印出的结果有错位,怎么调整呢?
作者: 吴超    时间: 2012-3-9 00:56
输出语句问题
  1. public class Demo {

  2.         public static void main(String[] args) {
  3.                 int num = 23;
  4.                 int height = num/4+1;
  5.                 int width = num;
  6.                
  7.                 int[][] arr = new int[height][width];
  8.                 int x = height-1;
  9.                 int y = 0;
  10.                 boolean count = true;
  11.                 for(int i=1; i<=width; i++){
  12.                         arr[x][y] = i;
  13.                         y++;
  14.                         if(count){
  15.                                 x--;
  16.                         }else{
  17.                                 x++;
  18.                         }
  19.                         if(x<0){
  20.                                 x=x+2;
  21.                                 count = false;
  22.                         }
  23.                         if(x>height-1){
  24.                                 x=x-2;
  25.                                 count = true;
  26.                         }               
  27.                 }
  28.                
  29.                 for(int i=0; i<arr.length; i++){
  30.                         for(int j=0; j<arr[i].length; j++){
  31.                                 if(arr[i][j]==0){
  32.                                         System.out.print("  ");//输出语句上的问题
  33.                                 }else{
  34.                                         System.out.print(arr[i][j]);
  35.                                 }
  36.                         }
  37.                         System.out.println();
  38.                 }
  39.         }
  40.         
  41. }
复制代码

1222222222224.jpg (7.64 KB, 下载次数: 48)

1222222222224.jpg

作者: 王睿    时间: 2012-3-9 13:31
谢谢哦!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2