Java 中的气压公式 - 帮助

发布于 2024-10-18 10:19:50 字数 1891 浏览 2 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

万劫不复 2024-10-25 10:19:50

错误消息准确地告诉您出了什么问题:您在使用之前没有初始化所有局部变量。所以就这么做吧。当你声明变量时,给它们一个默认值。

class Foo {
   public static void main(String[] args) {
     int foo = 0;  // initialized local variable
   }
}

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
   }
}
早茶月光 2024-10-25 10:19:50

在声明时将 PbTbLbHb 设置为 0,以便获得默认值。

您知道您的代码永远不会获得 h 的输入,这将使它们无法设置,但编译器不会。担心他们可能永远不会被安排!

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!

紫﹏色ふ单纯 2024-10-25 10:19:50

因为 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.

止于盛夏 2024-10-25 10:19:50

更详细:您的多案例分支的这个分支是罪魁祸首:

    if (h<0){
        ans = "error";
    }

如果发生这种情况,变量 Pb、Tb、Lb、Hb 未初始化,但您仍在 showMessageDialog 中使用它们。因此,在这种情况下进行初始化就足够了:

    if (h<0){
        ans = "error";
        Pb = 0;
        Tb = 0;
        Lb = 0;
        Hb = 0;
    }

更好的方法是在此处抛出异常(稍后捕获它),但如果您尚未了解异常,请先以简单的方式进行操作。


与您当前的问题无关,但是您的程序可以写得更好,而无需使用像这样的数组的长“if-then-else-if-...”语句:

// 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);

这避免了未初始化变量问题如果出现错误,请提前返回。

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.

年少掌心 2024-10-25 10:19:50

双Pb、Tb、Lb、Hb;

更改为

双Pb = 0.0,Tb = 0.0,Lb = 0.0,Hb
= 0.0;

double Pb, Tb, Lb, Hb;

change to

double Pb =0.0, Tb = 0.0, Lb = 0.0, Hb
= 0.0;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文