如何对齐我的阵列,使其看起来不那么糟糕?
换句话说,我如何格式化一个可读的表格?
这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;
}
}