Java 我需要帮助使用嵌套for循环创建此形状[更新]
我需要使用嵌套for loops for打印出此形状,以完成作业(完全公开) 但是我不知道怎么把整个东西放在中心 这些时期代表了模式的延续。所以它应该是整个金字塔 这是我现在所拥有的Java 我需要帮助使用嵌套for循环创建此形状[更新],java,nested-loops,Java,Nested Loops,我需要使用嵌套for loops for打印出此形状,以完成作业(完全公开) 但是我不知道怎么把整个东西放在中心 这些时期代表了模式的延续。所以它应该是整个金字塔 这是我现在所拥有的 public static void question4(){ int ix = 30; for(int i = 1; i<=128; i=i*2){ // x is the number printed //it gets the value from i, for (int g =
public static void question4(){
int ix = 30;
for(int i = 1; i<=128; i=i*2){
// x is the number printed
//it gets the value from i,
for (int g = ix; g>=0; g--){
System.out.print(" ");
}
for (int x2 =1; x2<=i-1; x2=x2*2){
System.out.print(" ");
System.out.print(x2);
}
for (int x = i; x>=1; x=x/2){
System.out.print(" ");
System.out.print(x);
}
ix=ix-4;
System.out.println();
publicstaticvoidquestion4(){
int ix=30;
对于(int i=1;i=0;g--){
系统输出打印(“”);
}
对于(整数x2=1;x2=1;x=x/2){
系统输出打印(“”);
系统输出打印(x);
}
ix=ix-4;
System.out.println();
}
感谢您对递减空格的帮助,现在数字本身将底部的行推到了上方。我尝试使用另一个用户建议的string.length命令,但它不断返回错误
试试这样的方法:
public static void main(String[] args) {
String spacer = " ";
for (int i = 1; i <= 128; i = i * 2) {
// x is the number printed
// it gets the value from i,
System.out.print(spacer);
for (int x2 = 1; x2 <= i - 1; x2 = x2 * 2) {
System.out.print(" ");
System.out.print(x2);
}
for (int x = i; x >= 1; x = x / 2) {
System.out.print(" ");
System.out.print(x);
}
if ((i * 2) < 10)
spacer = spacer.substring(0, spacer.length() - 2);
else if ((i * 2) < 100)
spacer = spacer.substring(0, spacer.length() - 3);
else
spacer = spacer.substring(0, spacer.length() - 4);
System.out.println();
}
}
我知道回答这个问题很容易。首先检查这个代码
int spaces = 7;
for(int i = 1; i<=128; i=i*2){
for(int k=1;k<=spaces;k++)
System.out.print(" ");
spaces--;
// x is the number printed
//it gets the value from i,
for (int x2 =1; x2<=i-1; x2=x2*2){
System.out.print(" ");
System.out.print(x2);
}
for (int x = i; x>=1; x=x/2){
System.out.print(" ");
System.out.print(x);
}
System.out.println();
}
int空格=7;
对于(inti=1;i,虽然已回答并接受,但这里有一个解决方案,可以打印一个完美的金字塔,其中一个为循环
public static void perfectPyramid() {
int upLimit = 1024;
int blankFieldWidth = String.valueOf(upLimit).length() + 1; // if upLimit is 3-digit, the blank field will be 4-blanks
String blank = new String(new char[blankFieldWidth]).replace("\0", " "); //one-liner for creating a String by repeating another String a given number of times
String numPart = "1" + new String(new char[blankFieldWidth - String.valueOf(blankFieldWidth - 1).length()]).replace("\0", " ");
String previous = "-"; // dummy initial value
for (int i = 1; i <= upLimit; i = i * 2) {
int countOfBlankFields = (int) (Math.log(upLimit / i) / Math.log(2)); // the count of blank columns per row (one side only)
String dynSpacer = new String(new char[blankFieldWidth - String.valueOf(i).length()]).replace("\0", " ");
numPart = numPart.replace(previous, previous + i + dynSpacer + previous);
String blanks = new String(new char[countOfBlankFields]).replace("\0", blank);
String row = blanks + numPart + blanks;
previous = i + dynSpacer;
System.out.println(row);
}
}
对于与嵌套循环有关的内容,您可以从通过替换字符串创建行移动到使用嵌套For。您需要设置一个间隔字符串,该间隔字符串在沿金字塔向下移动时递减。您会注意到,该间隔字符串的长度将与数字字符串的长度成反比。您应该能够计算For loop基于此观察尝试System.out.print not println。即使你这样做,它也会运行,但三角形仍然不正确。@brso05 lol这只是print
和println
的一个愚蠢错误。我的快速键入总有一天会杀了我:PI最终会模仿你的代码。它在循环中比另一个更好。这很奇怪,因为因为我们还没有学过子串之类的东西。
public static void perfectPyramid() {
int upLimit = 1024;
int blankFieldWidth = String.valueOf(upLimit).length() + 1; // if upLimit is 3-digit, the blank field will be 4-blanks
String blank = new String(new char[blankFieldWidth]).replace("\0", " "); //one-liner for creating a String by repeating another String a given number of times
String numPart = "1" + new String(new char[blankFieldWidth - String.valueOf(blankFieldWidth - 1).length()]).replace("\0", " ");
String previous = "-"; // dummy initial value
for (int i = 1; i <= upLimit; i = i * 2) {
int countOfBlankFields = (int) (Math.log(upLimit / i) / Math.log(2)); // the count of blank columns per row (one side only)
String dynSpacer = new String(new char[blankFieldWidth - String.valueOf(i).length()]).replace("\0", " ");
numPart = numPart.replace(previous, previous + i + dynSpacer + previous);
String blanks = new String(new char[countOfBlankFields]).replace("\0", blank);
String row = blanks + numPart + blanks;
previous = i + dynSpacer;
System.out.println(row);
}
}
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 512 256 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 512 1024 512 256 128 64 32 16 8 4 2 1