【问题标题】:How to Align A Table in Java如何在 Java 中对齐表格
【发布时间】:2016-10-21 21:20:19
【问题描述】:

如何对齐我的 Array 使其看起来不那么糟糕?

换句话说,我如何格式化一个可读的表格?

System.out.print(" "); 是一种非常低效的表格间距方式...

public class TaylorSin {
   public static void main(String[] args ) {
   double[][] ArrayX = new double[201][4];
   for (int i = 0; i < ArrayX.length; i++) { 
      System.out.print(ArrayX[i][0] = - 1 + (i * 0.01));
        System.out.print(" ");
        System.out.print(ArrayX[i][1] = Math.sin(- 1 + (i * 0.01)));
        System.out.print(" ");
        System.out.print(ArrayX[i][2] = taylorSin(- 1 + (i * 0.01)));
        System.out.print(" ");
        System.out.print(ArrayX[i][3] = Math.abs(Math.sin(- 1 + (i * 0.01)) - taylorSin(- 1 + (i * 0.01))));

            System.out.println();
        }       
    }

        public static int calculateFactorial(int n)             
        {       
            int facto = 1;      
            for (int i = 1; i <= n; i++)        
            {facto = facto * i;}        
            return facto;           
        }

        public static double calculateExponent(double base, int exponent)           
        {   
            if(exponent == 0){return 1;}   
            else {return base * calculateExponent(base, exponent - 1);}                     
        }       

        public static double calculateTerm(double base, int exponent, int n)            
        {           
            double term = 0.0;          
            term = (calculateExponent(base, exponent)/calculateFactorial(n));               
            return term;            
        }

        public static double sumOfTerms(double base, int exponent, int n)           
        {               
            double summation = 0;               
            for (int i = 1; i <= 21; i = i +2)          
            {               
                if (i == 3 || i == 7 || i == 11 || i == 15 || i == 19)              
                {                   
                summation = summation - calculateTerm(base, i, i);                                      
                }                   
                else                    
                {                   
                summation = summation + calculateTerm(base, i, i);                  
                }               
            }           
        return summation;           
        }

        public static double taylorSin(double base)         
        {           
        double result = 0;          
        int exponent = 1;
        int n = 1;          
        result = sumOfTerms(base, exponent, n);                     
        return result;          
        }           
}

【问题讨论】:

  • 使用System.out.printfSystem.out.format,它类似于(但不等于)C printf函数。
  • 如何获得输出。尝试使用\t
  • @asifsid88 如果你想让它像一张桌子一样工作,你不要使用\t,而是为每一列设置一个特定的大小,比如%20d%15s
  • @LuiggiMendoza 谢谢 :) 这增加了我的知识!赞赏

标签: java arrays alignment


【解决方案1】:

您可以使用System.out.printf 像这样格式化您的输出:

public class TaylorSin {
    public static void main(String[] args)
    {
        // How to align my Array so it does not look that awful?
        // In other words, How do I format a table that is readable?
        // The System.out.print(" "); is a pretty inefficient manner for spacing
        // for a table...
        double[][] ArrayX = new double[201][4];

        for (int i = 0; i < ArrayX.length; i++)

        {
            System.out.printf("%-15f", ArrayX[i][0] = -1 + (i * 0.01));
            System.out.print(" ");

            System.out
                    .printf("%-15f", ArrayX[i][1] = Math.sin(-1 + (i * 0.01)));
            System.out.print(" ");

            System.out.printf("%-15f",
                    ArrayX[i][2] = taylorSin(-1 + (i * 0.01)));
            System.out.print(" ");

            System.out.printf(
                    "%-15f",
                    ArrayX[i][3] = Math.abs(Math.sin(-1 + (i * 0.01))
                            - taylorSin(-1 + (i * 0.01))));

            System.out.println();
        }

    }

    public static int calculateFactorial(int n)

    {

        int facto = 1;

        for (int i = 1; i <= n; i++)

        {
            facto = facto * i;
        }

        return facto;

    }

    public static double calculateExponent(double base, int exponent)

    {

        if (exponent == 0) {
            return 1;
        }

        else {
            return base * calculateExponent(base, exponent - 1);
        }

    }

    public static double calculateTerm(double base, int exponent, int n)

    {

        double term = 0.0;

        term = (calculateExponent(base, exponent) / calculateFactorial(n));

        return term;

    }

    public static double sumOfTerms(double base, int exponent, int n)

    {

        double summation = 0;

        for (int i = 1; i <= 21; i = i + 2)

        {

            if (i == 3 || i == 7 || i == 11 || i == 15 || i == 19)

            {

                summation = summation - calculateTerm(base, i, i);

            }

            else

            {

                summation = summation + calculateTerm(base, i, i);

            }

        }

        return summation;

    }

    public static double taylorSin(double base)

    {

        double result = 0;

        int exponent = 1;
        int n = 1;

        result = sumOfTerms(base, exponent, n);

        return result;

    }
}

【讨论】:

    【解决方案2】:

    使用java.io.PrintStream.printf( format , data... ) 格式化打印

    格式字符串在Format string syntax中描述

    您可以使用的标志格式是width%-20s%12.4f

    【讨论】:

      【解决方案3】:

      试试这个。此外,如果可以的话,您可能需要格式化数字。

          { 
              System.out.print(ArrayX[i][0] = - 1 + (i * 0.01));
              System.out.print("\t");
      
              System.out.print(ArrayX[i][1] = Math.sin(- 1 + (i * 0.01)));
              System.out.print("\t");
      
              System.out.print(ArrayX[i][2] = taylorSin(- 1 + (i * 0.01)));
              System.out.print("\t");
      
              System.out.print(ArrayX[i][3] = Math.abs(Math.sin(- 1 + (i * 0.01)) -   taylorSin(- 1 + (i * 0.01))));
              System.out.print("\t");
              System.out.println("\n");
          }
      

      【讨论】:

      • 在问题中查看@LuiggiMendoza 评论
      • @Iswanto San 同意了。它发生在几分钟内:)
      • 谢谢,非常感谢!
      【解决方案4】:

      您可以使用 Java 的内置 System.out.printf() 并查看 Formatter 类。你可以这样做。

      System.out.printf("%f %f %f %f\n", ArrayX[i][0], ArrayX[i][1], ArrayX[i][1], ArrayX[i][2], ArrayX[i][3]);
      

      【讨论】:

        猜你喜欢
        • 2012-11-02
        • 1970-01-01
        • 1970-01-01
        • 2012-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-23
        • 2016-09-23
        相关资源
        最近更新 更多