[英](Java Number Pyramid) How do I get my numbers to have more spaces between them while lining up accurately when it is an integer is > 9?
I am having difficulty getting the desired output. 我很难获得所需的输出。 I know there are problems that are similar to mine that is already posted, but I find it hard to relate my code to their solutions without a massive overhaul. 我知道有些问题已经和我已经发布的问题类似,但是我发现很难在不进行大修的情况下将我的代码与其解决方案联系起来。
The solution for my class assignment: 我的课堂作业的解决方案:
Supposed to continue until every direction of pyramid equals 1 My second method "spaces" is redundant and I am not sure why. 应该继续下去,直到金字塔的每个方向都等于1。我的第二种方法“空格”是多余的,我不确定为什么。 Any help would be appreciated. 任何帮助,将不胜感激。
Blockq Blockq
public static void main(String[] args) {
numPar();
spaces();
}
private static void spaces() {
int x = 0;
if(x > 0 && x < 10) {
System.out.print(" ");
} else if (x > 10 && x < 99) {
System.out.print(" ");
} else if (x > 99) {
System.out.print(" ");
}
}
private static void numPar() {
int spaces = 14;
for(int i = 0; i<= 7; i++) {
for(int u = 0; u<spaces; u++) {
System.out.print(" ");
}
spaces--;
spaces--;
for(int j = 0 ; j <i ; j++) {
System.out.print(""+ (int) Math.pow(2,j)+" ");
}
for(int k = i ; k >=0 ; k--) {
System.out.print(""+ (int) Math.pow(2,k)+" ");
}
System.out.println("");
}
}
}
I made every number take 3 places using String.format("%3s", (int) Math.pow(2, j))
. 我使用String.format("%3s", (int) Math.pow(2, j))
使每个数字占据3位。 You can make it dynamic by replacing the number 3
here with the length of the largest number you'll print. 您可以通过将此处的数字3
替换为要打印的最大数字的长度来使其动态化。 I also changed the number of spaces in your print statements. 我还更改了打印语句中的空格数。 Here is the full code that prints an evenly spaced pyramid:- 这是打印均匀分布的金字塔的完整代码:-
public static void main(String[] args) {
numPar();
spaces();
}
private static void spaces() {
int x = 0;
if (x > 0 && x < 10) {
System.out.print(" ");
} else if (x > 10 && x < 99) {
System.out.print(" ");
} else if (x > 99) {
System.out.print(" ");
}
}
private static void numPar() {
int spaces = 14;
for (int i = 0; i <= 7; i++) {
for (int u = 0; u < spaces; u++) {
System.out.print(" ");
}
spaces--;
spaces--;
for (int j = 0; j < i; j++) {
System.out.print("" + String.format("%3s", (int) Math.pow(2, j)) + " ");
}
for (int k = i; k >= 0; k--) {
System.out.print("" + String.format("%3s", (int) Math.pow(2, k)) + " ");
}
System.out.println("");
}
}
String.format
explanation :- String.format
解释 :-
String.format("%3s", str)
will print the string str
, padding it with spaces, to make the total length 3 if it's less than 3. Note that you can write anything instead of 3 - I used 3 because your biggest number was of length 3. String.format("%3s", str)
将打印字符串str
,并用空格填充,以使总长度3(如果小于3的话)。请注意,您可以写任何东西来代替3-我使用3是因为您最大数字长度为3。
So "A" will be printed as "_ _ A" (2 spaces), and "Ab" will be printed as "_ Ab" (1 space). 因此,“ A”将被打印为“ _ _ A”(2个空格),而“ Ab”将被打印为“ _ Ab”(1个空格)。
I just replaced str
with your Math.pow(2, j)
. 我只是用您的Math.pow(2, j)
替换了str
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.