Barometric Formula in Java - Help
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?
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.
class Foo {
public static void main(String[] args) {
int foo = 0; // initialized local variable
}
}
Set Pb
, Tb
, Lb
, and Hb
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!
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.
More detailed: This branch of your multiple-case-branching is the culprit:
if (h<0){
ans = "error";
}
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:
if (h<0){
ans = "error";
Pb = 0;
Tb = 0;
Lb = 0;
Hb = 0;
}
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:
// minima of the intervals
double[] Hbs = { 0.0, 11000.0, 20000.0, 32000.0, 47000.0, 51000.0, 71000.0 };
// parameters for each interval
double[] Pbs = {101325.0, 22632.1, 5474.89, 868.019, 110.906, 66.9389, 3.95642 };
double[] Tbs = { 288.15, 216.65, 216.65, 228.65, 270.65, 270.65, 214.65 };
double[] Lbs = { -0.0065, 0.0, 0.001, 0.0028, 0.0, -0.0028, -0.002 };
int h = ...
if(h < 0) {
JOptionPane.showMessageDialog(null, "Don't use negative heights! (like " + h + ")", JOprionPane.ERROR_MESSAGE);
return;
}
// find out in which interval is our height
int i = Hbs.length-1;
while(h < Hbs[i]) {
i--;
}
double Hb = Hbs[i], Pb = Pbs[i], Tb = Tbs[i], Lb = Lbs[i];
double exp = ((-g0 * M * (h-Hb))/R * Tb);
double press = Pb*Math.exp(exp);
JOptionPane.showMessageDialog(null, "The answer is " +press+" Pascals", "Barometric Formula", JOptionPane.PLAIN_MESSAGE);
This avoids the uninitialized variable problem by returning early in case of an error.
double Pb, Tb, Lb, Hb;
change to
double Pb =0.0, Tb = 0.0, Lb = 0.0, Hb = 0.0;
精彩评论