0

首先,我对 Java 和编程都是新手(Matlab 除外),因此非常感谢简单的答案:-)。

我正在尝试创建一个温度转换器(带有 GUI),我需要更新一些标签。一开始效果很好,但现在我必须使用 if 语句中的值。这会导致我尝试更新标签的错误:

tempKelvin 无法解析为变量

单击“转换”按钮时会发生所有操作,代码如下:

// Create and add convert button
    JButton fahrenheitButton = new JButton("Convert");
    fahrenheitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            // Check if input is of type double and perform action
            if (isNumber(tempTextField.getText())) {
                double  inputTemp = Double.parseDouble(tempTextField.getText());

                // Convert from Kelvin
                if (((String) unitDropdown.getSelectedItem()).equals("Kelvin")) {
                    int tempKelvin     = (int) (inputTemp);
                    int tempCelcius    = (int) (inputTemp - 273.15);
                    int tempFahrenheit = (int) ((inputTemp - 273.15) * (9/5)  + 32);

                // Convert from Celsius
                } else if (((String) unitDropdown.getSelectedItem()).equals("Celsius")) {
                    int tempKelvin     = (int) (inputTemp + 273.15);
                    int tempCelcius    = (int) (inputTemp);
                    int tempFahrenheit = (int) (inputTemp  * (9/5)  + 32);

                // Convert from Fahrenheit
                } else if (((String) unitDropdown.getSelectedItem()).equals("Fahrenheit")) {
                    int tempKelvin     = (int) ((inputTemp - 32) * (5/9) + 273.15);
                    int tempCelcius    = (int) ((inputTemp - 32) * (5/9));
                    int tempFahrenheit = (int) ((inputTemp - 273.15) * (9/5)  + 32);

                // If none of the above was selected, it's an error...
                } else {
                    int tempKelvin = 0;
                    int tempCelcius = 0;
                    int tempFahrenheit = 0;
                    warningLabel.setText("Oops, this doesn't look good!");
                }

                // Update labels
                kelvinLabel.setText(tempKelvin + " K");
                celsiusLabel.setText(tempCelcius + " C");
                fahrenheitLabel.setText(tempFahrenheit + " F");
                warningLabel.setText("");


            } else {
                warningLabel.setText("Input must be numeric!");
            }
        }
    });

    fahrenheitButton.setBounds(20, 45, 89, 23);
    contentPane.add(fahrenheitButton);

任何帮助将不胜感激,谢谢!!

4

5 回答 5

0

您的问题是您在每个 if [else] 语句中创建一个新的临时变量。这就是为什么变量在这些语句之外不存在的原因。

于 2013-06-02T16:17:54.737 回答
0

您正在引用超出其范围的“临时”变量。它们在 if/else 条件语句的每个语句中声明,但在条件语句关闭后引用(在注释“更新标签”下)。

一种解决方案是在条件语句之前声明变量,并且只在条件中分配它们。

于 2013-06-02T16:18:08.340 回答
0

你的神奇词是:范围。因为您的变量是在您的 if 语句中定义的,所以它实际上在该语句之后“消失”(如果您愿意,则为“范围”) - 结束。

只需将您的变量提取到您想要引用它的最外部范围。祝您好运!

于 2013-06-02T16:19:47.503 回答
0

你最好这样做:

int tempKelvin     = 0;
int tempCelcius    = 0;
int tempFahrenheit = 0;

if(condition1){
    ///
}else if(condition2){
    //
}else if(condition3){
    //
}else{
    //
}

在 if 里面,不需要重新声明 int 函数,只需 tempKelvin=....;

于 2013-06-02T16:20:07.437 回答
0

您需要在 if 语句之外定义 int tempKelvin 并重用它,如下所述:

// Create and add convert button
    JButton fahrenheitButton = new JButton("Convert");
    fahrenheitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            // Check if input is of type double and perform action
            if (isNumber(tempTextField.getText())) {
                double  inputTemp = Double.parseDouble(tempTextField.getText());

                int tempKelvin = -1;
                int tempCelcius = -1;
                int tempFahrenheit = -1;
                // Convert from Kelvin
                if (((String) unitDropdown.getSelectedItem()).equals("Kelvin")) {
                    tempKelvin    = (int) (inputTemp);
                    tempCelcius    = (int) (inputTemp - 273.15);
                    tempFahrenheit = (int) ((inputTemp - 273.15) * (9/5)  + 32);

                // Convert from Celsius
                } else if (((String) unitDropdown.getSelectedItem()).equals("Celsius")) {
                    tempKelvin     = (int) (inputTemp + 273.15);
                    tempCelcius    = (int) (inputTemp);
                    tempFahrenheit = (int) (inputTemp  * (9/5)  + 32);

                // Convert from Fahrenheit
                } else if (((String) unitDropdown.getSelectedItem()).equals("Fahrenheit")) {
                    tempKelvin     = (int) ((inputTemp - 32) * (5/9) + 273.15);
                    tempCelcius    = (int) ((inputTemp - 32) * (5/9));
                    tempFahrenheit = (int) ((inputTemp - 273.15) * (9/5)  + 32);

                // If none of the above was selected, it's an error...
                } else {
                    tempKelvin = 0;
                    tempCelcius = 0;
                    tempFahrenheit = 0;
                    warningLabel.setText("Oops, this doesn't look good!");
                }

                // Update labels
                kelvinLabel.setText(tempKelvin + " K");
                celsiusLabel.setText(tempCelcius + " C");
                fahrenheitLabel.setText(tempFahrenheit + " F");
                warningLabel.setText("");


            } else {
                warningLabel.setText("Input must be numeric!");
            }
        }
    });

    fahrenheitButton.setBounds(20, 45, 89, 23);
    contentPane.add(fahrenheitButton);
于 2013-06-02T16:20:51.520 回答