我试图在同一条线上打印一个正方形和一个三角形,如下所示:
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
我已经创建了单独制作它们的功能
public static void drawTriangle(int n) {
int k = 2 * n - 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
System.out.print(" ");
}
k = k - 1;
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void drawSquare(int width, int height) {
for (int i = 0; i < height; i++) {
System.out.print("* ");
}
System.out.println();
for (int i = 0; i < width - 2; i++) {
System.out.print("* ");
for (int j = 0; j < height - 2; j++) {
System.out.print("* ");
}
System.out.println("* ");
}
for (int i = 0; i < height; i++) {
System.out.print("* ");
}
System.out.println();
}
但我不知道如何将两个输出组合在同一条线上。
答案 0 :(得分:3)
最简单的解决方案是将2种方法合并为1,如下所示:
public static void drawTriangleAndSquare(int widthS, int heightS) {
// number of leading spaces in front of triangle
int k = 2 * heightS - 5;
// print square
for (int i = 0; i < heightS; i++) {
for (int j = 0; j < widthS; j++) {
System.out.print("* ");
}
// print triangle
System.out.print("\t");
for (int j = 0; j < k; j++) {
System.out.print(" ");
}
k--;
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
drawTriangleAndSquare(5, 4);
}
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
请注意,我已经清理了一些代码,尤其是drawSquare()
函数。
我的实现仅允许您打印相同高度的正方形和三角形(它们都取决于heightS
中的变量drawTriangleAndSquare()
)。
<强> P.S。如果您想在三角形和正方形之间添加更多空格,只需在\t
添加更多System.out.print("\t")
。
答案 1 :(得分:2)
修改您的方法,使它们不打印,而是构建List<String>
。之后,您可以轻松地将两个输出合并为一个。
最大的优点是这种方法更加模块化。您可以以任何方式或与任何其他方法结合使用。
以下是您将如何使用它:
List<String> squareLines = drawSquare(4, 4);
List<String> triangleLines = drawTriangle(4);
// Iterate both lists
Iterator<String> squareLineIter = squareLines.iterator();
Iterator<String> triangleLineIter = triangleLines.iterator();
while (squareLineIter.hasNext() && triangleLineIter.hasNext()) {
String squareLine = squareLineIter.next();
String triangleLine = triangleLineIter.next();
// Print the combined line
System.out.println(squareLine + " " + triangleLine);
}
请注意,这会遗漏较长列表的剩余行。您可以通过用空行填充较小的列表来轻松支持不同的大小。
因此,修改您的方法以构建并返回列表。
创建结果,最初使用
清空List<String> lines = new ArrayList<>();
使用StringBuilder
StringBuilder lineBuilder = new StringBuilder();
lineBuilder.append("* "); // Repeat that in a loop
String line = lineBuilder.toString();
使用
将line
附加到lines
lines.add(line);
以下是您完全修改过的方法:
public static List<String> drawTriangle(int n) {
// Build the lines
List<String> lines = new ArrayList<>();
int k = 2 * n - 5;
for (int i = 0; i < n; i++) {
// Build the current line
StringBuilder lineBuilder = new StringBuilder();
for (int j = 0; j < k; j++) {
lineBuilder.append(" ");
}
k = k - 1;
for (int j = 0; j <= i; j++) {
lineBuilder.append("* ");
}
// Add the line
lines.add(lineBuilder.toString());
}
return lines;
}
public static List<String> drawSquare(int width, int height) {
// Build the lines
List<String> lines = new ArrayList<>();
// Build a line
StringBuilder lineBuilder = new StringBuilder();
for (int i = 0; i < height; i++) {
lineBuilder.append("* ");
}
// Add the line
lines.add(lineBuilder.toString());
for (int i = 0; i < width - 2; i++) {
// Build the current line
lineBuilder = new StringBuilder();
lineBuilder.append("* ");
for (int j = 0; j < height - 2; j++) {
lineBuilder.append("* ");
}
lineBuilder.append("* ");
// Add the line
lines.add(lineBuilder.toString());
}
// Build a line
lineBuilder = new StringBuilder();
for (int i = 0; i < height; i++) {
lineBuilder.append("* ");
}
// Add the line
lines.add(lineBuilder.toString());
return lines;
}