为什么java将我的双打识别为“未初始化”?

时间:2014-08-11 07:54:03

标签: java

我有一个关于java初始化的问题,我不知道为什么java认为我的程序中的double没有初始化,即使我已经初始化它。有人可以向我解释如何解决这个问题吗?在下面的代码中,java表示双“inC”未初始化。

package tempconverter;

import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.BorderLayout;

public class TempConverter extends JFrame implements ActionListener, ItemListener
{
    static String[] choices = { "From Celsius", "From Fahrenheit", "From Kelvin", "From Rankine" };
    static JFrame f = new JFrame("Temperature Converter");
    static JTextField enter = new JTextField(5);
    static JButton confirm = new JButton("Convert");
    static JComboBox choose = new JComboBox(choices);
    static JFrame tell = new JFrame();
    static JLabel one = new JLabel();
    static JLabel two = new JLabel();
    static JLabel three = new JLabel();

    public static void main(String[] args)
    {
        Font main = new Font("Comic Sans", Font.PLAIN, 12);
        confirm.addActionListener(new TempConverter());
        confirm.setFont(main);
        choose.setFont(main);
        enter.setFont(main);
        choose.addItemListener(new TempConverter());
        f.setBackground(Color.WHITE);
        f.setLayout(new FlowLayout());
        f.setSize(340, 60);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(enter);
        f.add(confirm);
        f.add(choose);
        tell.setLayout(new BorderLayout());
        tell.add(one, BorderLayout.NORTH);
        tell.add(two, BorderLayout.WEST);
        tell.add(three, BorderLayout.SOUTH);
        tell.setSize(160, 65);
        tell.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        double toConvert = Double.parseDouble(enter.getText());
        double inF, inK, inC, tempF = 0, tempK, inR, tempR;
        double tempC;

        if (choose.getSelectedItem().equals("From Celsius"))
        {
            tempF = toConvert * 1.8 + 32;
            tempK = toConvert + 273.15;
            tempR = (toConvert + 273.15) * 1.8;
            inF = Math.round(tempF * 100.00) / 100.00;
            inK = Math.round(tempK * 100.00) / 100.00;
            inR = Math.round(tempR * 100.00) / 100.00;
            one.setText(toConvert + "°C = " + inF + "°F");
            two.setText(toConvert + "°C = " + inK + "°K");
            three.setText(toConvert + "°C = " + inR + "°R");
            tell.setVisible(true);
        }

        if (choose.getSelectedItem().equals("From Fahrenheit"))
        {
            if (toConvert == 32)
            {
                inC = 0;
                inK = 273.15;
                inR = toConvert + 459.67;
                one.setText(toConvert + "°F = " + inC + "°C");
                two.setText(toConvert + "°F = " + inK + "°K");
                three.setText(toConvert + "°F = " + inR + "°R");
            }
            else
                tempC = (toConvert - 32) / 1.8;
            tempK = (toConvert - 32) / 1.8 + 273.15;
            tempR = toConvert + 459.67;
            inC = Math.round(tempC * 100.00) / 100.00;
            inK = Math.round(tempK * 100.00) / 100.00;
            inR = Math.round(tempR * 100.00) / 100.00;
            one.setText(toConvert + "°F = " + inC + "°C");
            two.setText(toConvert + "°F = " + inK + "°K");
            three.setText(toConvert + "°F = " + inR + "°R");
            tell.setVisible(true);
        }

        if (choose.getSelectedItem().equals("From Kelvin"))
        {
            tempC = toConvert - 273.15;
            tempF = tempC * 1.8 + 32;
            tempR = toConvert * 1.8;
            inC = Math.round(tempC * 100.00) / 100.00;
            inF = Math.round(tempF * 100.00) / 100.00;
            inR = Math.round(tempR * 100.00) / 100.00;
            one.setText(toConvert + "°K = " + inC + "°C");
            two.setText(toConvert + "°K = " + inF + "°F");
            three.setText(toConvert + "°K = " + inR + "°R");
            tell.setVisible(true);
        }

        if (choose.getSelectedItem().equals("From Rankine"))
        {
            if (toConvert == 0)
            {
                tempF = toConvert - 459.67;
                tempK = toConvert * (5 / 9);
                inF = Math.round(tempF * 100.00) / 100.00;
                inR = Math.round(tempK * 100.00) / 100.00;
                one.setText("0°R = -273.15°C");
                two.setText("0°R = " + inF + "°F");
                three.setText("0°R = " + inR + "°K");
                tell.setVisible(true);
            }
            else
                tempC = (toConvert - 491.67) * (5 / 9);
            tempF = toConvert - 459.67;
            tempK = toConvert * (5 / 9);
            inC = Math.round(tempC * 100.00) / 100.00;
            inF = Math.round(tempF * 100.00) / 100.00;
            inR = Math.round(tempK * 100.00) / 100.00;
            one.setText(toConvert + "°R = " + inC + "°C");
            two.setText(toConvert + "°R = " + inF + "°F");
            three.setText(toConvert + "°R = " + inR + "°K");
            tell.setVisible(true);
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e)
    {
        repaint();
    }
}    

Java指出我没有在“From Fahrenheit”和“From Rankine”方法的else语句中初始化“inC”。但是,当我在变量声明中初始化它时,当我尝试在if-else语句和方法中更改它时它不会改变。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

我复制了你的例子,我在tempC收到了消息。原因是在else路径上缺少}

更改:

   else
        tempC = (toConvert - 491.67) * (5 / 9);
        tempF = toConvert - 459.67;
        tempK = toConvert * (5 / 9);
        inC = Math.round(tempC * 100.00) / 100.00;
        inF = Math.round(tempF * 100.00) / 100.00;
        inR = Math.round(tempK * 100.00) / 100.00;
        one.setText(toConvert + "°R = " + inC + "°C");
        two.setText(toConvert + "°R = " + inF + "°F");
        three.setText(toConvert + "°R = " + inR + "°K");
        tell.setVisible(true);

TO:

           else {
                tempC = (toConvert - 491.67) * (5 / 9);
                tempF = toConvert - 459.67;
                tempK = toConvert * (5 / 9);
                inC = Math.round(tempC * 100.00) / 100.00;
                inF = Math.round(tempF * 100.00) / 100.00;
                inR = Math.round(tempK * 100.00) / 100.00;
                one.setText(toConvert + "°R = " + inC + "°C");
                two.setText(toConvert + "°R = " + inF + "°F");
                three.setText(toConvert + "°R = " + inR + "°K");
                tell.setVisible(true);
}

答案 1 :(得分:0)

如上所述,最简单的方法是在声明它时简单地为其赋值。

Java认为它尚未初始化的原因是它实际上可能没有。您只需在4个最外层if语句之一中为其赋值。如果这四个条件都不成立,则永远不会初始化变量。

正如Jens所指出的那样,你错过了{" else"。

答案 2 :(得分:0)

错误的实际原因是沉闷的:你错过了一个大括号。

但是,让我们看一下tempK = toConvert * (5 / 9);。这不会做你想要的,因为(5/9)将评估为0,因为括号表示这是自己评估的,是整数算术。要解决这个问题,最简单的方法是删除括号:

tempK = toConvert * 5 / 9;

所以一切都是以浮点计算的。 (请注意,*/具有相同的优先级。)

您在程序中多次出现此错误。