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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© CC_gogo 中级黑马   /  2014-10-3 10:33  /  1672 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

求解,如何用java写出一个矩形边框,例子如下:
*******
*        *
*        *
*        *
*******

4 个回复

倒序浏览
/*
思路:1、需求规律为第一行与最后一行打印所有列,其他行只打印第一列和最后一列。
2、将所有行分为两部分:(1)第一行与最后一行为一部分;(2)其他行为一部分。
*/
class Demo1
{
        public static void main(String[] args)
        {
                int i,j;
                for(i=0;i<5;i++){
                        for(j=0;(i==0||i==4)&j<5;j++){
                                System.out.print("*");
                        }
                        for(j=0;(i!=0&&i!=4)&j<5;j++){
                                if(j==0||j==4){
                                        System.out.print("*");
                                }
                                else{
                                        System.out.print(" ");
                                }
                        }
                        System.out.println();
                }
        }
}

评分

参与人数 1技术分 +1 收起 理由
敏敏好学 + 1

查看全部评分

回复 使用道具 举报
  1. /*
  2. *******
  3. *     *
  4. *     *
  5. *     *
  6. *******
  7. */
  8. class Temp
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.         DrawRec dr = new DrawRec(5,7);
  13.         dr.drawRec();
  14.     }
  15. }
  16. class DrawRec
  17. {
  18.     private int side;
  19.     private int wide;
  20.     DrawRec(int side,int wide)
  21.     {
  22.         this.side = side;
  23.         this.wide = wide;
  24.     }
  25.     public void drawRec()
  26.     {
  27.         for (int x=0; x<side; x++)
  28.         {
  29.             for (int y=0; y<wide; y++)
  30.             {
  31.                 if(x!=0 && x!=side-1)
  32.                                 {
  33.                                         if(y==0||y==wide-1)
  34.                                                 System.out.print("*");
  35.                                         else
  36.                                                 System.out.print(" ");
  37.                                 }
  38.                                 else
  39.                                 {
  40.                                         System.out.print("*");
  41.                                 }
  42.             }
  43.             System.out.println();
  44.         }
  45.     }
  46. }
复制代码
回复 使用道具 举报
  1. class Juxing
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 method(20,30);
  6.         }
  7.        
  8.         public static void method(int length,int width)
  9.         {
  10.                 Rect.widOutter(width);
  11.                 for(int i=1;i<length-1;i++)
  12.                         Rect.widInner(width);
  13.                 Rect.widOutter(width);
  14.         }
  15.        
  16. }
  17. class Rect
  18. {
  19.         public static void widOutter(int width)
  20.         {
  21.                 String s=new String(new char[width]);
  22.                 System.out.println(s.replace('\u0000','*'));
  23.         }
  24.         public static void widInner(int width)
  25.         {
  26.                 String s=new String(new char[width-2]);
  27.                 System.out.println("*"+s.replace('\u0000',' ')+"*");
  28.         }

  29. }
复制代码
回复 使用道具 举报
打印矩形边框
  1. package com.itheima.番外;

  2. public class 打点 {
  3.             public static void main(String[] args) {
  4.                 int x = 5;
  5.                 int y = 8;
  6.                 String[][] ii = new String[x][y];
  7.                 for (int i = 0; i <x; i++) {
  8.                         for (int j = 0; j <y; j++) {
  9.                                 if ((i == 0) || (i ==x - 1) || (j == 0) || (j ==y-1)) {
  10.                                         ii[i][j] = "*\t";
  11.                                 }else {
  12.                                         ii[i][j] ="\t";
  13.                                 }
  14.                         }
  15.                 }

  16.                 for (int i = 0; i < ii.length; i++) {
  17.                         for (int j = 0; j < ii[i].length; j++) {
  18.                                 System.out.print(ii[i][j]);
  19.                         }
  20.                         System.out.println();
  21.                 }
  22.         }

  23. }
复制代码


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马