A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 全海波 中级黑马   /  2012-7-22 13:37  /  2519 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 全海波 于 2012-8-4 09:45 编辑


我要的是空菱形!!!!
同时也打印个空正方形?
我希望不要用这种:
System.out.println("*******");
System.out.println("*           *");
System.out.println("*******");
输出语句来打印正方形?这种很弱智!这种方式打印

2 个回复

倒序浏览

回帖奖励 +3

本帖最后由 凉宫蛋蛋 于 2012-7-22 15:14 编辑
  1. package Heima001_DumpAndDown200;


  2. public class Test3 {
  3.     public static void main(String[] args)  {
  4.         //自定义行列
  5.         int row = 7;
  6.         int colunm = 13;
  7.         int [][] a = new int [row][colunm];
  8.         for(int i = 0, j = 0; i < row;i++){
  9.          

  10.             
  11.             int head = 0;

  12.             //元素行与列位置相同,遍历列,遍历长度小于最右边元素长度
  13.             for(j = i; j < colunm - i;j++){
  14.                
  15.                 //当元素下标等于3,到达空白处;下标等于最右边元素位置-3,可以到达空白处
  16.                 if(head == 0 || head == (colunm - 1) -2*i){
  17.                     a[i][j] = 1;
  18.                 }else{
  19.                     a[i][j] = 0;
  20.                 }
  21.                 //前面下标++,换行时,重置为0
  22.                 head++;               
  23.             }        
  24.         }

  25.         //倒置输出上半部
  26.         for(int i = row - 1; i > 0; i--){
  27.             for(int j = 0; j < colunm; j++){
  28.                 if(a[i][j] == 1){
  29.                     System.out.print("*");
  30.                 }else if(a[i][j] == 0 ){
  31.                     System.out.print(" ");
  32.                 }
  33.             }
  34.             System.out.println();            
  35.         }
  36.         //输出下半部
  37.         for(int i = 0; i < row; i++){
  38.             for(int j = 0; j < colunm; j++){
  39.                 if(a[i][j] == 1){
  40.                     System.out.print("*");
  41.                 }else if(a[i][j] == 0 ){
  42.                     System.out.print(" ");
  43.                 }
  44.             }
  45.             System.out.println();            
  46.         }
  47.     }
  48. }


复制代码




这个还是比较简单的,稍微难一点的可以参考我这里发过的帖子
http://bbs.itheima.com/forum.php?mod=viewthread&tid=20122


评分

参与人数 1技术分 +1 收起 理由
黑马张扬 + 1

查看全部评分

回复 使用道具 举报

  1. <P>/*
  2. -----*
  3. ----* *
  4. ---*   *
  5. --*     *
  6. -*       *
  7. *         *
  8. -*       *
  9. --*     *
  10. ---*   *
  11. ----* *
  12. -----*
  13. */</P>
  14. <P>class Test
  15. {
  16.         public static void main(String[] args)
  17.     {</P>
  18. <P>    System.out.println("-----*");//第一行的星号
  19.               
  20.                  for(int x=0;x<=4;x++)//从第二行开始循环
  21.                  {
  22.                          for(int y = x;y<4;y++)
  23.                          {
  24.                                  System.out.print("-");//
  25.          
  26.                          }
  27.        System.out.print("*");//左边的星号</P>
  28. <P>                         for(int z = 0;z<x*2+1;z++ )//两个星号间的空格数量递增</P>
  29. <P>      {</P>
  30. <P>        System.out.print(" ");</P>
  31. <P>      }  
  32.       
  33.        System.out.println("*"); //输出右边的星号同时换行
  34.                  }
  35.                  
  36.                  
  37.                
  38.     for(int x=4;x>0;x--)</P>
  39. <P>    {</P>
  40. <P>      for(int y = 4;y>=x;y--)</P>
  41. <P>      {</P>
  42. <P>       System.out.print("-");</P>
  43. <P>      }</P>
  44. <P>      System.out.print("*");</P>
  45. <P> </P>
  46. <P>      for(int z = 0;z<x*2-1;z++ )//两个星号间空格数量递减</P>
  47. <P>      {</P>
  48. <P>       System.out.print(" ");</P>
  49. <P>      }</P>
  50. <P>                        </P>
  51. <P>      System.out.println("*");
  52.                 }</P>
  53. <P>                 System.out.println("-----*");//最下边的空格
  54.                  
  55.             
  56.     }
  57.             
  58. }</P>
  59. <P> </P>
复制代码

你看这样是否符合你的要求
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马