2016-02-11 72 views
-2

除了我的主要课程,我想输出带有翼型点到命令行的表格,但现在一些系统打印功能不起作用。这里是我的计算类:某些打印功能不能在此计算类中运行。为什么?

package airfoil; 
import java.text.DecimalFormat; 
import java.text.NumberFormat; 

public class airfoil 
{ 
    private static final int numOfCoord = 250; 
    double dx = 1.0/numOfCoord; 

    private double  m;  // maximum camber in % of chord 
    private double  p;  // chordwise position of max ord., 10th of chord 
    private double  t;  // thickness in % of the cord 

    private String  nacaNum;  // NACA number - 4 digits 
    private double[][] coordinates; // Coordinates of the upper half 
             // or lower half of the airfoil 
    private double[][] meanLine;  // mean line coordinates 

    public airfoil(String number) { 

     nacaNum = number; 
     m = Double.parseDouble(nacaNum.substring(0,1))/100.0; 
     p = Double.parseDouble(nacaNum.substring(1,2))/10.0; 
     t = Double.parseDouble(nacaNum.substring(2,4))/100.0; 

     meanLine = new double[2][numOfCoord]; // x values row 0, y values row 1 

     // x upper = row 0, 
     // y upper = row 1, 
     // x lower = row 2, 
     // y lower = row 3 
     coordinates = new double [4][numOfCoord]; 

     System.out.println("NACA: " + nacaNum); 
     System.out.println("Number of coordinates: " + numOfCoord); 

     calcMeanLine(); 
     calcAirfoil(); 
    } 

    /* 
    * Calculates the values for the mean line forward of the maximum 
    * ordinate and aft of the maximum ordinate. 
    */ 
    private void calcMeanLine() { 

     double x = dx; 
     int j = 0; 

     // fwd of max ordinate 
     while (x <= p) { 
      meanLine[0][j] = x; 
      meanLine[1][j] = (m/(p * p))*(2*p*x - (x*x)); 
      x += dx; 
      j++; 
     } 

     // aft of max ordinate 
     while (x <= 1.0 + dx) { 
      meanLine[0][j] = x; 
      meanLine[1][j] = (m/((1 - p) * (1 - p))) * 
          ((1 - 2*p) + 2*p*x - x * x); 
      x += dx; 
      j++; 
     } 
    } // end calcMeanLine 

    /* 
    * Calculate the upper and lower coordinates of the airfoil surface. 
    */ 
    private void calcAirfoil() { 

     double theta;  // arctan(dy_dx) 
     double dy;   // derivative of mean line equation 
     double yt, ml;  // thickness and meanline values, respectively 
     double x = dx;  // x-value w.r.t. chord 
     int j = 0;   // counter for array 

     // calculate upper/lower surface coordinates fwd of max ordinate 
     while (x <= p) { 

      dy = (m/(p*p)) * (2*p - 2*x); 
      theta = Math.atan(dy); 
      yt = thicknessEQ(x); 
      ml = meanLine[1][j]; 

      // upper surface coordinates; 
      coordinates[0][j] = x - yt * Math.sin(theta); 
      coordinates[1][j] = ml + yt * Math.cos(theta); 

      // lower surface coordinates 
      coordinates[2][j] = x + yt*Math.sin(theta); 
      coordinates[3][j] = ml - yt * Math.cos(theta); 

      x += dx; 
      j++; 
     } 

     // calculate the coordinates aft of max ordinate 
     while (x <= 1.0 + dx) { 

      dy = (m/((1 - p) * (1 - p))) * ((2 * p) - (2 * x)); 
      theta = Math.atan(dy); 

      yt = thicknessEQ(x); 
      ml = meanLine[1][j]; 

      // upper surface coordinates; 
      coordinates[0][j] = x - yt * Math.sin(theta); 
      coordinates[1][j] = ml + yt * Math.cos(theta); 

      // lower surface coordinates 
      coordinates[2][j] = x + yt * Math.sin(theta); 
      coordinates[3][j] = ml - yt * Math.cos(theta); 

      x += dx; 
      j++; 
     } 
     System.out.println("j = " + j); 
    } // end calcAirfoil 

    /* 
    * Thickness equation 
    */ 
    private double thicknessEQ(double x) { 

     return ((t/0.2) * (0.2969 * Math.sqrt(x) - (0.126 * x) - 
       (0.3526 * x * x) + (0.28430 * x * x * x) - 
       (0.1015 * x * x * x * x))); 
    } 

    public String toString() { 

     String str = ""; 
     NumberFormat df = new DecimalFormat("0.0000"); 

     System.out.println("Xu\tYu\tXl\tYl"); 

      for (int j = 0; j < numOfCoord; j++) { 
       str += df.format(coordinates[0][j]) + "\t" + 
         df.format(coordinates[1][j]) + "\t" + 
         df.format(coordinates[2][j]) + "\t" + 
         df.format(coordinates[3][j]) + "\n"; 
      } 

     return str; 
    } 

    /* 
    * Return the coordinates array 
    */ 
    public double[][] getCoordinates() { return coordinates; } 
    public int getSize() { return numOfCoord; } 

} // end Airfoil class 

类的这部分应该打印表,但它不是做任何事情:

public String toString() { 

    String str = ""; 
    NumberFormat df = new DecimalFormat("0.0000"); 

    System.out.println("Xu\tYu\tXl\tYl"); 

    for (int j = 0; j < numOfCoord; j++) { 
     str += df.format(coordinates[0][j]) + "\t" + 
       df.format(coordinates[1][j]) + "\t" + 
       df.format(coordinates[2][j]) + "\t" + 
       df.format(coordinates[3][j]) + "\n"; 
    } 
    return str; 
} 

所以我能做些什么让这些事情正确打印?

+0

要打印的只是一条线这个字符串'“Xu \ tYu \ tXl \ tYl”'你可能想要添加这个'变量赋值后的'for'内的System.out.println'。里面有STR变量。 –

+2

你在哪里调用toString()?如果你不打电话,它无法打印。 – rajah9

+0

dx = 1.0/numOfCoord;他只是格式严重 – HopefullyHelpful

回答

0

您的System.out.println("Xu\tYu\tXl\tYl");代码会打印以下内容:Xu Yu Xl Yl因为您没有向其中添加任何变量。你可以将其更改为:

public String toString() { 
    String str = ""; 
    NumberFormat df = new DecimalFormat("0.0000"); 

     for (int j = 0; j < numOfCoord; j++) { 
      str += df.format(coordinates[0][j]) + "\t" + 
        df.format(coordinates[1][j]) + "\t" + 
        df.format(coordinates[2][j]) + "\t" + 
        df.format(coordinates[3][j]) + "\n"; 
     } 

    System.out.println(str); 
    return str; 
} 

反正不toString()功能打印。 toString()不应该打印任何东西,它应该只是返回字符串,以便您可以打印它或做任何你想要的东西。

Airfoil airfoil = new Airfoil(); // Yes, first letter uppercase recommended 
// do stuff 
System.out.println(airfoil.toString()); 

编辑:这对我的作品(我认为)。我使用输入1000(顺便说一句,你应该使用try/catch来处理3个或更少字符的数字,否则它会崩溃),这是我得到的输出(只有开始和结束),因为它很长。

NACA: 1000 
Number of coordinates: 250 
j = 250 
0.0040 0.0100 0.0040 0.0100 
0.0080 0.0100 0.0080 0.0100 
0.0120 0.0100 0.0120 0.0100 
[...] 
0.9920 0.0002 0.9920 0.0002 
0.9960 0.0001 0.9960 0.0001 
1.0000 -0.0000 1.0000 -0.0000 

这是用来做toString()代码它的工作:

public String toString() { 
    String str = ""; 
    NumberFormat df = new DecimalFormat("0.0000"); 

    // System.out.println("Xu\tYu\tXl\tYl"); 

    for (int j = 0; j < numOfCoord; j++) { 
     str += df.format(coordinates[0][j]) + "\t" + 
       df.format(coordinates[1][j]) + "\t" + 
       df.format(coordinates[2][j]) + "\t" + 
       df.format(coordinates[3][j]) + "\n"; 
    } 
    return str; 
} 

我主要使用的:

public static void main (String args[]){ 
    Airfoil air = new Airfoil("1000"); 
    System.out.println(air.toString()); 
} 
+0

添加system.out.println(str);编译并重新运行代码后不做任何事情。 Xu Yu X1 Y1应该是格式化的,Xu用于翼型下的x坐标,Yu用于翼型下的坐标等等 –

+0

你是最好的。非常感谢你!不确定为什么'机翼翼型=新翼型();'一开始没有工作。也许是因为“机翼”这个名字在其他一些名字上有问题。我将常量“1000”更改为nacaNum,这是我的扫描仪输入的影响,因此该文件现在完全可用。我可能会在不久的将来实现GUI,并且可以导出为.txt。不应该那么辛苦。谢谢! –

相关问题