0

それほどひどく見えないように配列を調整するにはどうすればよいですか?

つまり、読み取り可能なテーブルをフォーマットするにはどうすればよいですか?

これ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;          
        }           
}
4

4 に答える 4

2

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;

    }
}
于 2013-02-25T06:52:50.387 に答える
1

java.io.PrintStream.printf(format、data ...)を使用してフォーマットされた印刷

フォーマット文字列は、フォーマット文字列構文で説明されています

使用できるフラグ形式はwidth%-20sまたは%12.4f

于 2013-02-25T06:51:48.663 に答える
0

Javaのbuit-inSystem.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]);
于 2013-02-25T06:56:25.813 に答える
0

これを試して。さらに、必要に応じて数値をフォーマットすることもできます。

    { 
        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");
    }
于 2013-02-25T06:53:45.623 に答える