Java 中的气压公式 - 帮助
我正在学习 Java,我想创建一个非常基本的计算器来计算海拔特定高度的气压。然而,每次我尝试构建代码时,我都会收到有关局部变量的错误......可能尚未初始化。
import javax.swing.JOptionPane;
import java.lang.Math;
class barometer {
public static void main(String args[]){
String fn = JOptionPane.showInputDialog("Enter Height Above Sea Level in Metres:");
double h = Integer.parseInt(fn);
double R = 8.31432;
double g0 = 9.80665;
double M = 0.0289644;
double Pb, Tb, Lb, Hb;
String ans = "";
if (h<0){
ans = "error";
} else if (h>=0 && h<11000){
Pb = 101325.0;
Tb = 288.15;
Lb = -0.0065;
Hb = 0.0;
} else if (h>=11000 && h<20000){
Pb = 22632.1;
Tb = 216.65;
Lb = 0.0;
Hb = 11000.0;
} else if (h>=20000 && h<32000){
Pb = 5474.89;
Tb = 216.65;
Lb = 0.001;
Hb = 20000.0;
} else if (h>=32000 && h<47000){
Pb = 868.019;
Tb = 228.65;
Lb = 0.0028;
Hb = 32000.0;
} else if (h>=47000 && h<51000){
Pb = 110.906;
Tb = 270.65;
Lb = 0.0;
Hb = 47000.0;
} else if (h>=51000 && h<71000){
Pb = 66.9389;
Tb = 270.65;
Lb = -0.0028;
Hb = 51000.0;
} else if (h>=71000){
Pb = 3.95642;
Tb = 214.65;
Lb = -0.002;
Hb = 71000.0;
}
double exp = ((-g0 * M * (h-Hb))/R * Tb);
double press = Pb*Math.exp(exp);
JOptionPane.showMessageDialog(null, "The answer is " +press+ans+"Pascals", "Barometric Formula", JOptionPane.PLAIN_MESSAGE);
}
}
那么这段代码有什么问题呢?
I'm learning Java and I wanted to create a very basic Calculator to calculate the air pressure at a certain height above sea level. However every time I try to build the code, I get an error about the local variable ... may not have been initialized.
import javax.swing.JOptionPane;
import java.lang.Math;
class barometer {
public static void main(String args[]){
String fn = JOptionPane.showInputDialog("Enter Height Above Sea Level in Metres:");
double h = Integer.parseInt(fn);
double R = 8.31432;
double g0 = 9.80665;
double M = 0.0289644;
double Pb, Tb, Lb, Hb;
String ans = "";
if (h<0){
ans = "error";
} else if (h>=0 && h<11000){
Pb = 101325.0;
Tb = 288.15;
Lb = -0.0065;
Hb = 0.0;
} else if (h>=11000 && h<20000){
Pb = 22632.1;
Tb = 216.65;
Lb = 0.0;
Hb = 11000.0;
} else if (h>=20000 && h<32000){
Pb = 5474.89;
Tb = 216.65;
Lb = 0.001;
Hb = 20000.0;
} else if (h>=32000 && h<47000){
Pb = 868.019;
Tb = 228.65;
Lb = 0.0028;
Hb = 32000.0;
} else if (h>=47000 && h<51000){
Pb = 110.906;
Tb = 270.65;
Lb = 0.0;
Hb = 47000.0;
} else if (h>=51000 && h<71000){
Pb = 66.9389;
Tb = 270.65;
Lb = -0.0028;
Hb = 51000.0;
} else if (h>=71000){
Pb = 3.95642;
Tb = 214.65;
Lb = -0.002;
Hb = 71000.0;
}
double exp = ((-g0 * M * (h-Hb))/R * Tb);
double press = Pb*Math.exp(exp);
JOptionPane.showMessageDialog(null, "The answer is " +press+ans+"Pascals", "Barometric Formula", JOptionPane.PLAIN_MESSAGE);
}
}
So what is wrong with this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
错误消息准确地告诉您出了什么问题:您在使用之前没有初始化所有局部变量。所以就这么做吧。当你声明变量时,给它们一个默认值。
The error message is telling you exactly what's wrong: You don't initialize all the local variables before use. So do it. When you declare your variables give them a default value.
在声明时将
Pb
、Tb
、Lb
和Hb
设置为 0,以便获得默认值。您知道您的代码永远不会获得 h 的输入,这将使它们无法设置,但编译器不会。担心他们可能永远不会被安排!
Set
Pb
,Tb
,Lb
, andHb
to 0 when they're declared so you have a default.You know your code will never get an input for h that will let them not get set, but the compiler doesn't. It's worried they might never get set!
因为 Pb、Tb、Lb、Hb 仅在 if 块内初始化,所以所有“if”条件可能永远不会出现,因此它们将在不被赋值的情况下出现在 if 块中。最后的“else”可能会有所帮助,或者在条件块之前给它们值。
Because Pb, Tb, Lb, Hb are only initialised inside if blocks, it's possible that all the 'if' conditions will never arise, and so they'll come out the if blocks without being assigned a value. A final 'else' could help there, or giving them values before the block of conditionals.
更详细:您的多案例分支的这个分支是罪魁祸首:
如果发生这种情况,变量 Pb、Tb、Lb、Hb 未初始化,但您仍在
showMessageDialog
中使用它们。因此,在这种情况下进行初始化就足够了:更好的方法是在此处抛出异常(稍后捕获它),但如果您尚未了解异常,请先以简单的方式进行操作。
与您当前的问题无关,但是您的程序可以写得更好,而无需使用像这样的数组的长“if-then-else-if-...”语句:
这避免了未初始化变量问题如果出现错误,请提前返回。
More detailed: This branch of your multiple-case-branching is the culprit:
If this occurs, the variables Pb, Tb, Lb, Hb are not initialized, yet you still are using them in the
showMessageDialog
. So it should be enough to put initializations in this case:A better way would be to throw an exception here (and catch it later), but if you didn't yet learn about exceptions, do it first the simple way.
Unrelated to your current problem, but your program could be written quite nicer without the long "if-then-else-if-..." statement using arrays like this:
This avoids the uninitialized variable problem by returning early in case of an error.
更改为
change to