我需要帮助让我的代码看起来像这样:

时间:2017-11-16 21:45:51

标签: java

我需要帮助让我的代码输出看起来像下面的图像,我不确定我做错了什么,我需要让我的代码输出缩进,并且不能记住如何缩进它。

这是我目前的代码:

// Fig. 18.3: FactorialCalculator.java
// Recursive factorial method.

public class Assignment_6_1 
{
    // recursive method factorial (assumes its parameter is >= 0
    static StringBuilder s = new StringBuilder();

    public static long factorial(long number) 
{
        if (number == 0) // test for base case
        {
            return 1; 
        } else {// recursion step         
            if (s.length() == 0) {
                s.append(number).append("*").append(number - 1);
            } else {
                for (int i = 0; i < s.length(); i++) {

                }
                System.out.println(number + " * " + (number - 1) + "!");
                s.append("*").append(number).append("*").append(number - 1);
            }
            return number * factorial(number - 1);
        }
    }

    // output factorials for values 0-21
    public static void main(String[] args) {
        System.out.println("Hannah Coffey - Lab 6");
        // calculate the factorials of 0 through 20
        for (int counter = 0; counter < 25; counter++) {
            s = new StringBuilder();
            System.out.println(counter + "!");
            System.out.printf("%d! = %d%n", counter, factorial(counter));
        }
    }
} // end class FactorialCalculator

这就是我需要的样子而不确定我做错了什么:

enter image description here

非常感谢任何帮助。

我的程序目前看起来像这样: enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用String填充String.format,如下所示:

String.format("%10s", "foo"); 

它将创建一个7个空格7 + foo.length = 10;

的左边距

现在,我们可以像以下一样呈现动态:

public static String padding( int i ) {
    return i == 0 ? "" : String.format( "%" + i + "s", "" );
}

只需传递空格数,然后让格式化程序完成工作。

for(int i = 5; i >= 0; --i){
    System.out.println(padding(i) + "i");
}

输出:

     5
    4
   3
  2
 1
0

答案 1 :(得分:-3)

 public static long factorial(long number){
         if (number == 0) // test for base case
         {
             return 1;
         } else {// recursion step
             if (s.length() == 0) {
                 s.append(number).append("*").append(number - 1);
             } else {
                 String spaces = "";
                 for (int i = 0; i < number; i++) {
                     spaces = spaces+" ";
                 }
                 if(number==1)
                    System.out.println(" Base Case: 1");
                 else {
                    System.out.println(spaces + "Recursive call:" + number + " * fact(" + (number - 1) + ")"); 
                    s.append("*").append(number).append("*").append(number - 1);
                 }
             }
        return number * factorial(number - 1);
         }
     }

public static void main(String[] args) {
    System.out.println("Hannah Coffey - Lab 6");
    // calculate the factorials of 0 through 20
    for (int counter = 0; counter < 25; counter++) {
        s = new StringBuilder();
        for (int i = 0; i < counter; i++) {
            spaces = spaces+" ";
        }
        System.out.println(spaces+"Recursive call:"+counter + "!");
        System.out.printf("%d! = %d%n", counter, factorial(counter));
    }
}