创建一个表格并将数据放入其中。

huangapple go评论79阅读模式
英文:

Create a table and put data inside

问题

如何为这段代码创建一个表格?

表格将显示n和结果。

类似于以下形式:

我知道可以使用System.out.print(.....)来打印它,但是否有更好的方法?

   fib(n)   result
     9        34
     10       55
     11       89
class fib
{
    static int fib(int n)
    {
        int f[] = new int[n+2]; 
        int i;

        f[0] = 0;
        f[1] = 1;

        for (i = 2; i <= n; i++)
        {
            f[i] = f[i-1] + f[i-2];
        }

        return f[n];
    }

    public static void main (String args[])
    {
        int a = 9;
        int b = 10;
        int c = 11;
        System.out.println(fib(a));
        System.out.println(fib(b));
        System.out.println(fib(c));
    }
}
英文:

How can I create a table for this code?

The table would show the n and the result.

Something that would look like

I know I can print it using System.out.print(.....) but it there a better way to do it?

      fib(n)   result
        9        34
        10       55
        11       89
      
class fib
{
    static int fib(int n)
    {
        int f[] = new int[n+2]; 
        int i;

    
        f[0] = 0;
        f[1] = 1;

        for (i = 2; i &lt;= n; i++)
        {
            f[i] = f[i-1] + f[i-2];
        }

        return f[n];
    }

    public static void main (String args[])
    {
        int a = 9;
        int b = 10;
        int c = 11;
        System.out.println(fib(a));
        System.out.println(fib(b));
        System.out.println(fib(c));


    }
} 

答案1

得分: 1

Right now your code only outputs the results of the fib method. The println method outputs the data you specify as a parameter and then ends the line.

In addition to println, the PrintStream class has many methods that allow you to do specific things with outputting text. One method you could use is print, which does the same as println but does not end the line. This means you can do:

System.out.print("\t"); // Print a tab character
System.out.print(a); // Print variable a
System.out.print("\t"); // Print another tab character
System.out.println(fib(a)); // Calculate fib(a), print the result, and end the line

Another interesting method is printf, which allows you to specify a "format String" that then gets populated with the values of the additional parameters passed to the method.

System.out.printf("\t%d\t%d%n", a, fib(a)); // Output the variable a and the result of fib(a), and end the line

Format Strings are a pretty broad subject, but the above example specifies that two tabs should be printed, with a decimal (%d) after the first tab and another one (the second %d) after the second tab. The next parameter (a) replaces the first decimal, the second parameter (the result of fib(a)) replaces the second decimal. The %n means "end the line".

You can output the header in a similar manner.

英文:

Right now your code only outputs the results of the fib method. The println method outputs the data you specify as parameter, and then ends the line.

In addition to println, the PrintStream class has many methods that allow you to do specific things with outputting text. One method you could use is print, which does the same as println but does not end the line. This means you can do:

System.out.print(&quot;\t&quot;); // Print a tab character
System.out.print(a); // Print variable a
System.out.print(&quot;\t&quot;); // Print another tab character
System.out.println(fib(a)); // Calculate fib(a), print the result, and end the line

Another interesting method is printf, which allows you to specify a "format String", that then gets populated with the values of the additional parameters passed to the method.

System.out.printf(&quot;\t%d\t%d%n&quot;, a, fib(a)); // Output the variable a and result of fib(a), and end the line

Format Strings are a pretty broad subject, but the above example specifies that two tabs should be printed, with a decimal (%d) after the first tab and another one (the second %d) after the second tab. The next parameter (a) replaces the first decimal, the second parameter (the result of fib(a)) replaces the second decimal. The %n means "end the line".

You can output the header in a similar manner.

huangapple
  • 本文由 发表于 2020年7月28日 15:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63128736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定