Java 每行之前需要越来越多的空格

Java 每行之前需要越来越多的空格,java,for-loop,Java,For Loop,我需要这个输出 5 4 3 2 1 0 我的守则如下: public static void outputEleven() { System.out.println(""); for(int x=5; x>=0; x--) { System.out.println(x); for(int s=0; s<=5; s++) { Sy

我需要这个输出

5   
 4     
  3 
   2 
    1 
     0 
我的守则如下:

public static void outputEleven()
{
    System.out.println("");
    for(int x=5; x>=0; x--)
    {   
        System.out.println(x);
        for(int s=0; s<=5; s++)
        {
            System.out.print(" ");
        }
    }

}

为什么我得到这个输出?如何获得所需的输出?

在内部循环中,您希望在打印值之前打印
I
空格。大概

for (int i = 0; i < 6; i++) {
    for (int j = 0; j < i; j++) {
        System.out.print(" ");
    }
    System.out.println(5 - i);
}
或者,对于相同的输出,像

for (int x = 5; x >= 0; x--) {
    for (int s = 0; s < 5 - x; s++) {
        System.out.print(" ");
    }
    System.out.println(x);
}
用于(int x=5;x>=0;x--){
对于(int s=0;s<5-x;s++){
系统输出打印(“”);
}
系统输出println(x);
}

在内部循环中,您希望在打印值之前打印
i
空格。大概

for (int i = 0; i < 6; i++) {
    for (int j = 0; j < i; j++) {
        System.out.print(" ");
    }
    System.out.println(5 - i);
}
或者,对于相同的输出,像

for (int x = 5; x >= 0; x--) {
    for (int s = 0; s < 5 - x; s++) {
        System.out.print(" ");
    }
    System.out.println(x);
}
用于(int x=5;x>=0;x--){
对于(int s=0;s<5-x;s++){
系统输出打印(“”);
}
系统输出println(x);
}

用于(int s=0;st这是有效的。这对我来说是一个非常简单的错误。谢谢!不客气。不过有一点警告,根据您的逻辑,它将在打印最后一个数字后打印一行空格。您可以通过在打印循环之前使用空格循环来避免这种情况,您应该能够使它在不做太多修改的情况下工作。@DPabst,以较少的时间复杂度和一个循环的使用来查看我的答案。
(int s=0;st这是有效的。这对我来说是一个非常简单的错误。谢谢!不客气。不过有一点警告,根据您的逻辑,它将在打印最后一个数字后打印一行空格。您可以通过在打印循环之前使用空格循环来避免这种情况,您应该能够使它在不做太多修改的情况下工作。@DPabst,检查我的答案,时间复杂度较低,使用一个循环。更多循环导致更复杂,可以使用System.out.format更好地完成。更多循环导致更复杂,可以使用System.out.format更好地完成。