英文:
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:
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 <= rows; i++) {
for (int j = 1; j <= (rows - i) * 2; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--) {
System.out.print(" " + k); //create left half
}
for (int l = 2; l <= i; l++) {
System.out.print(" " + 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 <= 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++;
}
答案3
得分: 0
你可以使用StringBuilder
来保存整个需要打印的字符串,并用空格填充它。
对于初始步骤,你将'1'
设置到中间,然后打印字符串的内容。
之后你向左和向右“移动”,设置下一个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 '1'
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++ <= width) {
sb.append(' '); // prepare StringBuilder with whitespaces
}
i = 0;
int left = width / 2;
int right = width / 2;
while (i++ < n - 1) { // print 1..8
sb.setCharAt(left, (char)(i + '0'));
sb.setCharAt(right, (char)(i + '0'));
System.out.println(sb);
left -= space;
right += space;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论