【发布时间】:2015-06-06 15:59:51
【问题描述】:
程序要打印符号的矩形的长度和宽度 例如
*****
* *
* *
* *
* *
*****
【问题讨论】:
程序要打印符号的矩形的长度和宽度 例如
*****
* *
* *
* *
* *
*****
【问题讨论】:
你可以通过你的逻辑来解决这个问题。你可以看到下面的源代码:
import java.util.Scanner;
public class Piramd1 {
public static void main(String args[]) {
Scanner conin = new Scanner(System.in);
System.out.print("How many lines=");
int n = conin.nextInt();
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n; c++) {
if (r == 1 || r == n || c == 1 || c == n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
【讨论】:
使用逻辑检查迭代为二维数组:
public static void rectOuter(int length, int width) {
String printStr = "*";
String seprator = " ";
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
if (i == 0 || j == 0 || i == length - 1 || j == width - 1)
System.out.print(printStr + seprator);
else
System.out.print(seprator + seprator);
System.out.println();
}
}
PS:System.out.print 被 StringBuilder 替换
【讨论】: