私有靜態int M = 20
私有靜態int N = 18;
私有int m;//長度
私有int n;//寬度
RectangleDemo(int m,int n) {
如果(m & gt0)this . m = m;
else this.m = M
如果(n & gt0)this . n = n;
else this.n = N
}
公共作廢打印(){
for(int I = 0;我& ltn;i++) {
if (i == 0 || i == n - 1) {
for(int j = 0;j & ltm;j++) {
if (j == 0 || j == m - 1)
system . out . print(" | ");
其他
system . out . print("-");
}
}否則{
for(int j = 0;j & ltm;j++) {
if (j == 0 || j == m - 1)
system . out . print(" | ");
其他
system . out . print(" ");
}
}
system . out . println();
}
}
公共靜態void main(String[] a) {
rectangle demo re = new rectangle demo(20,8);
re . print();
}
}
結果
如果要以' * '為邊,就把輸出改成*。