英文:
Adding gradually decreasing spaces in java with nested for loops in this java program, beginner question
问题
public class nested4loops {
public static void main(String[] args) {
timething();
}
public static void timething() {
System.out.println("|\u0022\u0022\u0022\u0022\u0022\u0022\u0022\u0022|"); // " instead of "
for (int i = 0; i < 5; i++) {
for (int s = 0; s < i; s++) {
System.out.print(" ");
}
System.out.print("\\");
for (int b = 7; b > i; b--) {
System.out.print(":");
}
System.out.println();
}
}
}
英文:
currently trying to write this in java, but i cant seem to get the spaces and the colons correct with for loops.
|"""""""""|
\:::::::/
\:::::/
\:::/
\:/
/:\
/:::\
/:::::\
/:::::::\
|"""""""""|
^ the thing im trying to write
my code currently :
public class nested4loops {
public static void main(String[] args) {
timething();
}
public static void timething() {
System.out.println("|\"\"\"\"\"\"\"\"\"|");
for (int i = 0; i < 5; i++) {
for (int s = 0; s < i; s++) {
System.out.print(" ");
}
System.out.print("\\");
for (int b = 7; b > i; b--) {
System.out.print(":");
}
System.out.println();
}
}
}
Currently however, this code prints:
|"""""""""|
\:::::::
\::::::
\:::::
\::::
\:::
答案1
得分: 1
一个方法是使用两个循环,一个用于顶部部分,一个用于底部部分。
然而,这意味着这些循环的主体被重复,违反了[DRY原则][1](不要重复自己)。
当然,可以通过将共同的逻辑移动到方法中来处理,例如:
```lang-java
public static void printHourGlass(int size) {
printEndRow(size);
for (int row = 1; row < size; row++)
printGlassRow(row, size, '\\', '/');
for (int row = size - 1; row > 0; row--)
printGlassRow(row, size, '/', '\\');
printEndRow(size);
}
private static void printEndRow(int size) {
System.out.println("|" + repeat('"', size * 2 - 1) + "|");
}
private static void printGlassRow(int row, int size, char left, char right) {
System.out.println(repeat(' ', row) + left + repeat(':', (size - row) * 2 - 1) + right);
}
private static String repeat(char ch, int count) {
char[] buf = new char[count];
Arrays.fill(buf, ch);
return new String(buf);
}
测试
printHourGlass(3);
printHourGlass(5);
printHourGlass(9);
输出
|"|
\:::/
\:/
/:\
/:::\
|"|
|"|
\::::::/
\::::/
\::/
\:/
/:\
/:::\
/::::/
/::::::\
|"|
|"|
\::::::::::::::/
\::::::::::::/
\::::::::::/
\::::::::/
\::::::/
\::::/
\::/
\:/
/:\
/:::\
/::::/
/::::::/
/::::::::/
/::::::::::/
/::::::::::::/
/::::::::::::::/
|"|
<details>
<summary>英文:</summary>
One way to do this, is to have 2 loops, one for the top part and one for the bottom part.
However, that means that the body of those loops are repeated, which violates the [DRY principle][1] (Don't Repeat Yourself).
That can of course be handled by moving the common logic to methods, e.g. like this:
```lang-java
public static void printHourGlass(int size) {
printEndRow(size);
for (int row = 1; row < size; row++)
printGlassRow(row, size, '\\', '/');
for (int row = size - 1; row > 0; row--)
printGlassRow(row, size, '/', '\\');
printEndRow(size);
}
private static void printEndRow(int size) {
System.out.println("|" + repeat('"', size * 2 - 1) + "|");
}
private static void printGlassRow(int row, int size, char left, char right) {
System.out.println(repeat(' ', row) + left + repeat(':', (size - row) * 2 - 1) + right);
}
private static String repeat(char ch, int count) {
char[] buf = new char[count];
Arrays.fill(buf, ch);
return new String(buf);
}
Tests
printHourGlass(3);
printHourGlass(5);
printHourGlass(9);
Outputs
|"""""|
\:::/
\:/
/:\
/:::\
|"""""|
|"""""""""|
\:::::::/
\:::::/
\:::/
\:/
/:\
/:::\
/:::::\
/:::::::\
|"""""""""|
|"""""""""""""""""|
\:::::::::::::::/
\:::::::::::::/
\:::::::::::/
\:::::::::/
\:::::::/
\:::::/
\:::/
\:/
/:\
/:::\
/:::::\
/:::::::\
/:::::::::\
/:::::::::::\
/:::::::::::::\
/:::::::::::::::\
|"""""""""""""""""|
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论