循环以打印数字模式未能打印正确的模式

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

For loop to print a numeric pattern does not print the correct pattern

问题

这是它应该看起来的样子:

    9 8 7 6 5 4 3 2 1 0

    8 8 7 6 5 4 3 2 1 0

    7 7 7 6 5 4 3 2 1 0

    6 6 6 6 5 4 3 2 1 0

    5 5 5 5 5 4 3 2 1 0

    4 4 4 4 4 4 3 2 1 0

    3 3 3 3 3 3 3 2 1 0

    2 2 2 2 2 2 2 2 1 0

    1 1 1 1 1 1 1 1 1 0

    0 0 0 0 0 0 0 0 0 0

这是我尝试的代码。

public class Main {
    public static void main(String[] args) {
        int i = 9;
        int count = 0; // 修改这里的初始值为0
        while (i >= count) {
            int j = i;
            while (j >= count) {
                System.out.print(j + " ");
                j--;
            }
            System.out.println();
            count++; // 将count的递减改为递增
        }
    }
}

实际输出应该是:

9 8 7 6 5 4 3 2 1 0 
8 7 6 5 4 3 2 1 0 
7 6 5 4 3 2 1 0 
6 5 4 3 2 1 0 
5 4 3 2 1 0 
4 3 2 1 0 
3 2 1 0 
2 1 0 
1 0 
0 

代码的问题在于初始值设置以及递增递减的方式。我已经对代码进行了修改,使其输出与期望输出一致。

英文:

This is what it should look like

9 8 7 6 5 4 3 2 1 0

8 8 7 6 5 4 3 2 1 0 

7 7 7 6 5 4 3 2 1 0

6 6 6 6 5 4 3 2 1 0

5 5 5 5 5 4 3 2 1 0

4 4 4 4 4 4 3 2 1 0

3 3 3 3 3 3 3 2 1 0

2 2 2 2 2 2 2 2 1 0

1 1 1 1 1 1 1 1 1 0

0 0 0 0 0 0 0 0 0 0

Here's my attempt.

public class Main {
	public static void main(String[] args) {
		int i = 9;
		int count = -1;
		while (i >= count) {
			int j = i;
			while (j > count) {
				System.out.print(j + " ");
				j--;
			}
			System.out.println();
			count++;
		}
	}
}

Here's my actual output:

9 8 7 6 5 4 3 2 1 0 
9 8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 
9 8 7 6 5 4 3 
9 8 7 6 5 4 
9 8 7 6 5 
9 8 7 6 
9 8 7 
9 8 
9 

This obviously does not match the expected output. Can someone point out where the mistake is in the code?

答案1

得分: 2

以下是翻译好的内容:

这是一个输出正确的解决方案,但是我使用了for循环,而不是while循环。

public class Main {
    public static void main(String[] args) {
        int count1 = 9;
        for (int i = count1; i >= 0; i--) {
            int count2 = i;
            if (count1 > count2) {
                int tmp = count1 - count2;
                for (int j = tmp; j > 0; j--) {
                    System.out.print(count2 + " ");
                }
            }
            for (int j = count2; j >= 0; j--) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}
英文:

This is a Solution that has the right output, but instead of using while-Loops I used for-Loops

public class Main {
    public static void main(String[] args) {
        int count1 = 9;
        for (int i = count1; i >= 0; i--) {
            int count2 = i;
            if (count1 > count2) {
                int tmp = count1 - count2;
                for (int j = tmp; j > 0; j--) {
                    System.out.print(count2 + " ");
                }
            }
            for (int j = count2; j >= 0; j--) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

答案2

得分: 1

你可以保留两个外部变量,countermultiplier,分别用于矩阵大小和重复次数的计数:

public class Main {
    public static void main(String[] args) {
        int counter = 15;
        int multiplier = 1;
        for (int i = counter; i >= 0; i--) {
            for (int j = 0; j < multiplier; j++) {
                System.out.printf("%3d", counter); // 使用 %3d 以便美观地排列数字
            }
            for (int k = counter - 1; k >= 0; k--) {
                System.out.printf("%3d", k);
            }
            ++multiplier;
            --counter;
            System.out.println();
        }
    }
}

对于每一行水平线,其中 counter 减少,multiplier 增加(第一行一次为9,第二行两次为8,依此类推):

  • 首先会打印 counter,重复 multiplier 次;
  • 然后会用 counter-multiplier 数量的降序整数填充行的其余部分,从 counter-1 开始;
  • 在外部循环的每次迭代结束时,会打印一个新行。

输出将会是:

9  8  7  6  5  4  3  2  1  0
8  8  7  6  5  4  3  2  1  0
7  7  7  6  5  4  3  2  1  0
6  6  6  6  5  4  3  2  1  0
5  5  5  5  5  4  3  2  1  0
4  4  4  4  4  4  3  2  1  0
3  3  3  3  3  3  3  2  1  0
2  2  2  2  2  2  2  2  1  0
1  1  1  1  1  1  1  1  1  0
0  0  0  0  0  0  0  0  0  0
英文:

You can keep two outer variables, counter and multiplier, for the matrix size and repetitions' count respectively:

public class Main {
    public static void main(String[] args) {
        int counter = 15;
        int multiplier = 1;
        for (int i = counter; i &gt;= 0; i--) {
            for (int j = 0; j&lt;multiplier; j++) {
                System.out.printf(&quot;%3d&quot;, counter); //using %3d for spacing numbers nicely
            }
            for (int k = counter-1; k &gt;= 0; k--) {
                System.out.printf(&quot;%3d&quot;, k);
            }
            ++multiplier;
            --counter;
            System.out.println();
        }
    }
}

For every horizontal line, where counter decreases, and multiplier increases (9 once on 1st line; 8 twice on the second line, etc.):

  • it will first print the counter, multiplier times;
  • it will then fill the rest of the line with counter-multiplier number of descending sequence integers, starting from counter-1;
  • at the end of outer loop's each iteration, a new line is printed.

Output would be:

9  8  7  6  5  4  3  2  1  0
8  8  7  6  5  4  3  2  1  0
7  7  7  6  5  4  3  2  1  0
6  6  6  6  5  4  3  2  1  0
5  5  5  5  5  4  3  2  1  0
4  4  4  4  4  4  3  2  1  0
3  3  3  3  3  3  3  2  1  0
2  2  2  2  2  2  2  2  1  0
1  1  1  1  1  1  1  1  1  0
0  0  0  0  0  0  0  0  0  0

答案3

得分: 0

既然你已经有了答案这里有几个替代方法
String str = "9 8 7 6 5 6 3 2 1 0";
System.out.println(str);
for (int i = 9; i > 0; i--) {
    str = str.replace(i+"",(i-1)+"");
    System.out.println(str);
}
或者使用 `String.repeat` 方法
for (int i = 9; i >= 0; i--) {
    System.out.print((i+" ").repeat(9-i));
    for(int k = i; k >= 0; k--) {
        System.out.print(k + " ");
    }
    System.out.println();
}
英文:

Since you already have your answer, here are a couple alternatives.

String str = &quot;9 8 7 6 5 6 3 2 1 0&quot;;
System.out.println(str);
for (int i = 9; i &gt; 0; i--) {
	str = str.replace(i+&quot;&quot;,(i-1)+&quot;&quot;);
	System.out.println(str);
}

Or use the String.repeatmethod.

for (int i = 9; i &gt;= 0; i--) {
	System.out.print((i+&quot; &quot;).repeat(9-i));
	for(int k = i; k &gt;= 0; k--) {
		System.out.print(k + &quot; &quot;);
	}
	System.out.println();
}

</details>



# 答案4
**得分**: 0

用Java8的流(Stream)功能,你可以将代码编写如下:

```java
public static void main(String[] args) {
    IntStream.range(0, 10)
        .forEach(i -> {
            IntStream.range(0, 10).forEach(j -> {
                System.out.print((9 - (j < i ? i : j)) + " ");
            });
            System.out.println("");
        });
}
英文:

With the help of Java8 stream you can write the code as below:

public static void main(String[] args) {
    	
    	IntStream.range(0, 10)
        .forEach(i -&gt; {
        	IntStream.range(0, 10).forEach(j -&gt; {
    			
    			System.out.print((9- (j &lt; i ? i : j)) + &quot; &quot; );
        		
        	});
        	System.out.println(&quot;&quot;);
        });
    }

huangapple
  • 本文由 发表于 2020年10月13日 21:28:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64336114.html
匿名

发表评论

匿名网友

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

确定