2016-02-11 73 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。不應該那麼辛苦。謝謝! –

相關問題