简单的就这样,不过dos里面打印出来有点难看!
- class DrawRect
- {
- int x,y;
- public DrawRect(int x,int y)
- {
- if(x<1||y<1)
- throw new RuntimeException("该矩形不存在!");
- this.x = x;
- this.y = y;
- }
- public void draw()
- {
- for(int i=0;i<y+2;i++)
- {
- for(int j=0;j<x;j++)
- {
- if(i==0||i==y+1)
- {
- System.out.print("-");
- }
- else if(j==0||j==x-1)
- {
- System.out.print("|");
- }
- else
- {
- System.out.print(" ");
- }
- }
- System.out.println("");
- }
- }
- public static void main(String[] args)
- {
- new DrawRect(4,6).draw();
- }
- }
复制代码 |