Java 某些打印函数不在此计算类中运行。为什么?
除了我的主类之外,我还想将带有翼型点的表格输出到命令行,但是现在一些系统打印功能不起作用。这是我的计算课:Java 某些打印函数不在此计算类中运行。为什么?,java,Java,除了我的主类之外,我还想将带有翼型点的表格输出到命令行,但是现在一些系统打印功能不起作用。这是我的计算课: package airfoil; import java.text.DecimalFormat; import java.text.NumberFormat; public class airfoil { private static final int numOfCoord = 250; double dx = 1.0 / numOfCoord; pri
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
package翼型;
导入java.text.DecimalFormat;
导入java.text.NumberFormat;
公共级机翼
{
私有静态最终整数NUMOFCORD=250;
双dx=1.0/numOfCoord;
私有双m;//最大拱度,以弦的%为单位
私有双p;//最大值的弦向位置,第10个弦
专用双t;//跳线的厚度(以百分比为单位)
私有字符串nacaNum;//NACA数字-4位
私有双精度[][]坐标;//上半部分的坐标
//或者机翼的下半部分
私有双[][]平均线;//平均线坐标
公共机翼(字符串编号){
nacaNum=数量;
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;
平均线=新的双精度[2][numOfCoord];//x值第0行,y值第1行
//x上限=第0行,
//y上=第1行,
//x下=第2行,
//y下=第3行
坐标=新的双[4][numOfCoord];
System.out.println(“NACA:+nacaNum”);
System.out.println(“坐标数:+numOfCoord”);
calcMeanLine();
跟骨();
}
/*
*计算最大值前的平均线值
*最大纵坐标的纵坐标和纵坐标。
*/
私有void calcMeanLine(){
双x=dx;
int j=0;
//最大纵坐标fwd
而(xYourSystem.out.println(“Xu\tYu\tXl\tYl”);
code打印以下内容: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;
}
编辑:对我来说很有用(我想)。我使用了输入1000
(顺便说一句,你应该对3个字符或更少的数字使用try/catch,否则它会崩溃),这是我得到的输出(只有开头和结尾),因为它很长
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;
}
您正在打印一行,其中包含此字符串“Xu\tYu\tXl\tYl”
,您可能希望在变量赋值后将此System.out.println
添加到for
的中。其中包含STR变量。您在哪里调用toString()?如果您不调用它,它就无法打印。dx=1.0/numOfCoord;他刚格式化的badlytoString方法不应该打印任何东西,而应该只构造对象字符串表示。打印操作应该在外部完成。因此,在要打印的主类中创建coordinates.toString();添加system.out.println(str);编译并重新运行代码后不做任何事情。Xu-Yu X1 Y1应该是格式化的,Xu代表翼型下的x坐标,Yu代表翼型下的坐标,等等。你是最好的。非常感谢!不确定为什么翼型=新翼型();
一开始没有工作。可能是因为名字“翼型”与其他一些名称有问题。我将常量“1000”更改为nacaNum,这是我的扫描仪输入所影响的,因此该文件现在功能齐全。在不久的将来,我可能会致力于实现GUI,并且导出为.txt.的能力不应该那么困难。谢谢!
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
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());
}