写一个Java程序,使用仅有的while循环打印一个数字金字塔。

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

Write a java program that prints a pyramid of numbers using only a while loop

问题

public class NumPyramid {

    public static void main(String[] args) {

        int rows = 9;

        int i = 1;
        while (i <= rows) {
            int j = 1;
            while (j <= (rows - i) * 2) {
                System.out.print(" ");
                j++;
            }
            
            int k = i;
            while (k >= 1) {
                System.out.print(" " + k); //create left half            
                k--;
            }
            int l = 2;
            while (l <= i) {
                System.out.print(" " + l); //create right half
                l++;
            }
            
            System.out.println();
            i++;
        }
    }
}
英文:

It needs to look like this:

写一个Java程序,使用仅有的while循环打印一个数字金字塔。

I have a for loop code that works but I can't turn it into a correct while loop one.

Here is the for loop code:

public class NumPyramid {

    public static void main(String[] args) {

        int rows = 9;

        for (int i = 1; i &lt;= rows; i++) {
            for (int j = 1; j &lt;= (rows - i) * 2; j++) {
                System.out.print(&quot; &quot;);
            }
            
            for (int k = i; k &gt;= 1; k--) {
                System.out.print(&quot; &quot; + k); //create left half            
            }
            for (int l = 2; l &lt;= i; l++) {
                System.out.print(&quot; &quot; + l); //create right half
            }
            
            System.out.println();
        }
    }
}

答案1

得分: 4

这是将 for 循环转换为 while 循环的一般方法:

for (initial; condition; iterate) {
    statement;
}

变成

initial;
while (condition) {
    statement;
    iterate;
}
英文:

In general, this is how you convert a for loop to a while loop:

for (initial; condition; iterate) {
    statement;
}

becomes

initial;
while (condition) {
    statement;
    iterate;
}

答案2

得分: 2

这是我所做的,而且它有效!

  int rows = 9, i = 1;

   while (i <= rows)
	{
	    int j = 1;
	    while (j<=(rows-i)*2)
	    {
	    	System.out.print(" ");
	    	j++;
	    }
	    int k = i;
	    while (k >= 1)
	    {
	    	System.out.print(" "+k);
	    	k--;
	    }
	    int l = 2;
	    while (l<=i)
	    {
	    	System.out.print(" "+l);
	    	l++;
	    }
	    System.out.println();
	    i++;
	}
英文:

This is what I did and it works!

  int rows = 9, i = 1;

   while (i &lt;= rows)
	{
	    int j = 1;
	    while (j&lt;=(rows-i)*2)
	    {
	    	System.out.print(&quot; &quot;);
	    	j++;
	    }
	    int k = i;
	    while (k &gt;= 1)
	    {
	    	System.out.print(&quot; &quot;+k);
	    	k--;
	    }
	    int l = 2;
	    while (l&lt;=i)
	    {
	    	System.out.print(&quot; &quot;+l);
	    	l++;
	    }
	    System.out.println();
	    i++;
	}

答案3

得分: 0

你可以使用StringBuilder来保存整个需要打印的字符串,并用空格填充它。

对于初始步骤,你将&#39;1&#39;设置到中间,然后打印字符串的内容。
之后你向左和向右“移动”,设置下一个i并重复打印,直到所有行都完成。

更新
添加了space参数,用于控制金字塔中数字之间的距离。

int n = 9;
int i = 1;
int space = 2; // 两个相邻数字之间的距离
int width = 2 * space * (n - 2) + 1; // 不包括 0

StringBuilder sb = new StringBuilder();
while (i++ <= width) {
    sb.append(' ');  // 使用空格预先填充 StringBuilder
}

i = 0;
int left = width / 2;
int right = width / 2;
while (i++ < n - 1) { // 打印 1 到 8
    sb.setCharAt(left, (char)(i + '0'));
    sb.setCharAt(right, (char)(i + '0'));
    System.out.println(sb);
    left -= space;
    right += space;
}
英文:

You may use a StringBuilder to hold the entire string you need to print and fill it with spaces.

For the initial step you set &#39;1&#39; into the middle and print the contents of the string.
After that you "move" to the left and right, set the next i and repeat printing until all rows are done.<br/>

Update<br/>
Added space parameter to control the distance between digits in the pyramid.

int n = 9;
int i = 1;
int space = 2; // distance between two adjacent digits
int width = 2 * space * (n - 2) + 1; // 0 not included

StringBuilder sb = new StringBuilder();
while (i++ &lt;= width) {
    sb.append(&#39; &#39;);  // prepare StringBuilder with whitespaces
}

i = 0;
int left = width / 2;
int right = width / 2;
while (i++ &lt; n - 1) { // print 1..8
    sb.setCharAt(left, (char)(i + &#39;0&#39;));
    sb.setCharAt(right, (char)(i + &#39;0&#39;));
    System.out.println(sb);
    left -= space;
    right += space;
}

huangapple
  • 本文由 发表于 2020年9月30日 01:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64124984.html
匿名

发表评论

匿名网友

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

确定