如何右对齐弗洛伊德 2 的三角幂的打印

如何在输出弗洛伊德三角形的最后 2 行中向左移动?这是我的输出:


      1 

     21 

    421 

   8421 

  168421 

 32168421 

预期输出:


         1 

        21 

       421 

      8421 

    168421 

  32168421 

这是我的代码:


 for(int i=0; i<=5; ++i) {


         //total of space

        int n=6-i;


         //print space

         while(n>0) {

             System.out.print(" ");

             n--;

         }


         //print number

         for(int j=i; j>=0; j--) {

             System.out.print((int)Math.pow(2, j));

         }


         System.out.println(" ");


     }

谢谢


青春有我
浏览 37回答 2
2回答

繁星淼淼

&nbsp; int lineLength = 8;&nbsp; &nbsp; for (int i = 0; i <= 5; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; //print number&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; for (int j = i; j >= 0; j--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append((int) Math.pow(2, j));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //print space&nbsp; &nbsp; &nbsp; &nbsp; for (int spaces = lineLength - sb.length(); spaces > 0; spaces--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sb.toString());&nbsp; &nbsp; }还有一个更通用的例子:public static void main(String[] args) {&nbsp; &nbsp; int numbersToCompute = 10;&nbsp; &nbsp; int lineLength = floydsNumber(numbersToCompute).length();&nbsp; &nbsp; for (int i = 0; i <= numbersToCompute; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; String floydsNumber = floydsNumber(i);&nbsp; &nbsp; &nbsp; &nbsp; for (int spaces = lineLength - floydsNumber.length(); spaces > 0; spaces--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(floydsNumber.toString());&nbsp; &nbsp; }}private static String floydsNumber(int i) {&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; for (int j = i; j >= 0; j--) {&nbsp; &nbsp; &nbsp; &nbsp; sb.append((int) Math.pow(2, j));&nbsp; &nbsp; }&nbsp; &nbsp; return sb.toString();}

哆啦的时光机

&nbsp; &nbsp; &nbsp; &nbsp;List<String> result = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<=5; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=i; j>=0; j--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append((int)Math.pow(2, j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(sb.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // You need to find out the longgest string for the padding left calculation&nbsp; &nbsp; &nbsp; &nbsp; int length = result.get(result.size() - 1).length();&nbsp; &nbsp; &nbsp; &nbsp; result.forEach((str -> System.out.println(padLeft(str, length))));为填充添加直到方法:public String padLeft(String inputString, int length) {&nbsp; &nbsp; &nbsp; &nbsp; if (inputString.length() >= length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return inputString;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; while (sb.length() < length - inputString.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(' ');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb.append(inputString);&nbsp; &nbsp; &nbsp; &nbsp; return sb.toString();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java