使用嵌套的for循环在Java程序中逐渐减少空格,这是一个初学者问题。

huangapple go评论65阅读模式
英文:

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.

|&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;|
 \:::::::/
  \:::::/
   \:::/
    \:/
    /:\
   /:::\
  /:::::\
 /:::::::\
|&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;|

^ 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(&quot;|\&quot;\&quot;\&quot;\&quot;\&quot;\&quot;\&quot;\&quot;\&quot;|&quot;);
		for (int i = 0; i &lt; 5; i++) {
			for (int s = 0; s &lt; i; s++) {
				System.out.print(&quot; &quot;);
			}
			System.out.print(&quot;\\&quot;);
			for (int b = 7; b &gt; i; b--) {
				System.out.print(&quot;:&quot;);
			}
			System.out.println();
		}
	}
}

Currently however, this code prints:

|&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;|
\:::::::
 \::::::
  \:::::
   \::::
    \:::

答案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&#39;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 &lt; size; row++)
		printGlassRow(row, size, &#39;\\&#39;, &#39;/&#39;);
	for (int row = size - 1; row &gt; 0; row--)
		printGlassRow(row, size, &#39;/&#39;, &#39;\\&#39;);
	printEndRow(size);
}

private static void printEndRow(int size) {
	System.out.println(&quot;|&quot; + repeat(&#39;&quot;&#39;, size * 2 - 1) + &quot;|&quot;);
}

private static void printGlassRow(int row, int size, char left, char right) {
	System.out.println(repeat(&#39; &#39;, row) + left + repeat(&#39;:&#39;, (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

|&quot;&quot;&quot;&quot;&quot;|
 \:::/
  \:/
  /:\
 /:::\
|&quot;&quot;&quot;&quot;&quot;|
|&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;|
 \:::::::/
  \:::::/
   \:::/
    \:/
    /:\
   /:::\
  /:::::\
 /:::::::\
|&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;|
|&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;|
 \:::::::::::::::/
  \:::::::::::::/
   \:::::::::::/
    \:::::::::/
     \:::::::/
      \:::::/
       \:::/
        \:/
        /:\
       /:::\
      /:::::\
     /:::::::\
    /:::::::::\
   /:::::::::::\
  /:::::::::::::\
 /:::::::::::::::\
|&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;|

huangapple
  • 本文由 发表于 2020年10月6日 06:16:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64216937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定