gpt4 book ai didi

java - 将循环打印到表中

转载 作者:行者123 更新时间:2023-12-02 05:27:00 25 4
gpt4 key购买 nike

我正在尝试将此循环打印到 N 10*N 100*N 等下的表格中。我可以使循环正常工作,但我无法计算出将值向右移动的间距。我对此比较陌生,因此我们将不胜感激。

package Exercises;

public class TabularOutput {

public static void main(String[] args)
{
//initialize variables
int w = 0;
int x = 0;
int y = 0;
int z = 0;

System.out.println("N 10*N 100*N 1000*N\n");

while(w<=4)
{
int a = w + 1;
System.out.println(a);
++w;
}
while(x<=4)
{
int b = (x + 1) * 10;
System.out.println(b);
++x;
}
while(y<=4)
{
int c = (y + 1) * 100;
System.out.println(c);
++y;
}
while(z<=4)
{
int d = (z + 1) * 1000;
System.out.println(d);
++z;
}


}//end method main

}//end class TabularOutput

最佳答案

试试这个:

 int a=0, b=0, c=0, d=0;

while(w<=4)
{
a = w + 1;
System.out.print(a);
++w;

b = (x + 1) * 10;
System.out.print("\t"+b);
++x;

c = (y + 1) * 100;
System.out.print("\t"+c);
++y;

d = (z + 1) * 1000;
System.out.println("\t"+d);
++z;

}

关于java - 将循环打印到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com