【问题标题】:Temperature Conversion: String Input to Double温度转换:字符串输入到双精度
【发布时间】:2013-11-14 22:28:45
【问题描述】:

// 我正在尝试使用 GUI 程序来转换温度。我为用户使用了一个 JTextField 来输入一串数字,因此我可以将该字符串字段转换为双精度,然后将该双精度用于公式,然后将其转换回字符串。我需要为这两种转换使用两种不同的方法。我对以下几行的转换方法有错误:

c = double.parseDouble(userInput); 
f = (c * 9 / 5) + 32);

&

f = double.parseDouble(userInput);
c = (f * 9 / 5) + 32);

这是我的代码:

public Temperature() {
    super( "Temperature Converter" ) ;
    setSize( HEIGHT, WIDTH ) ;
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;

    JPanel panel1 = new JPanel() ;
    panel1.setLayout(new GridLayout(2,5));

    labelC = new JLabel("Celsius:");
    panel1.add(labelC);

    textFieldC = new JTextField( "Enter Here") ;
    panel1.add(textFieldC) ;

    labelEquals = new JLabel("=");
    panel1.add(labelEquals);

    textFieldAnswerF = new JTextField( "Answer") ;
    panel1.add(textFieldAnswerF) ;

    convertC2F = new JButton( "C to F") ;
    panel1.add(convertC2F) ;

    labelF = new JLabel("Fahrenheit:");
    panel1.add(labelF);

    textFieldF = new JTextField( "Enter Here") ;
    panel1.add(textFieldF) ;

    labelEquals = new JLabel("=");
    panel1.add(labelEquals);

    textFieldAnswerC = new JTextField( "Answer") ;
    panel1.add(textFieldAnswerC) ;

    convertF2C = new JButton( "F to C") ;
    panel1.add(convertF2C) ;

    add(panel1) ;

} // Temperature constructor

/**
    Method to convert Celsius to Fahrenheit
*/
public double fahrenheit(double f, double c){
    String  userInput = textFieldC.getText();
    c = double.parseDouble(userInput); 
    f = (c * 9 / 5) + 32);
    textFieldAnswerF.setText("" + f);
}

/**
    Method to convert Fahrenheit to Celsius
*/
public double celsius(double c, double f){
    String  userInput = textFieldF.getText();
    f = double.parseDouble(userInput);
    c = (f * 9 / 5) + 32);
    textFieldAnswerC.setText("" + c);
}

这是我的错误:

Temperature.java:87: error: class expected
    c = double.parseDouble(userInput);
               ^
Temperature.java:87: error: ';' expected
    c = double.parseDouble(userInput);
                          ^
Temperature.java:87: error: not a statement
    c = double.parseDouble(userInput);
                           ^
Temperature.java:87: error: ';' expected
    c = double.parseDouble(userInput);
                                    ^
Temperature.java:88: error: ';' expected
    f = (c * 9 / 5) + 32);
                        ^
Temperature.java:97: error: class expected
    f = double.parseDouble(userInput);
               ^
Temperature.java:97: error: ';' expected
    f = double.parseDouble(userInput);
                          ^
Temperature.java:97: error: not a statement
    f = double.parseDouble(userInput);
                           ^
Process javac exited with code 1
Temperature.java:97: error: ';' expected
    f = double.parseDouble(userInput);
                                    ^
Temperature.java:98: error: ';' expected
    c = (f * 9 / 5) + 32);
                        ^
10 errors

【问题讨论】:

  • 错误信息是什么?这实际上很重要,你知道...
  • 我已经把我的错误信息放了

标签: java string user-interface type-conversion


【解决方案1】:

Java 区分大小写。使用

f = Double.parseDouble(userInput);

另外在这两种情况下从第二个语句中删除额外的括号:

c = (f * 9 / 5) + 32;
                    ^

【讨论】:

  • 谢谢,我已经修复了所有问题,但它要求返回声明。我应该把什么作为回报?
  • 如果我返回 c,那不是只给我双倍而不是字符串吗?我需要返回一个字符串,以便它在我的 JTextField 中显示一个字符串
  • 然后在返回值上使用Double.toString
  • 什么意思?这在我的程序中看起来如何?我在方法中的最后几行是 c = (f - 32) * 5/9; textFieldAnswerC.setText("" + c);返回 c;
  • 不要在方法本身中设置文本。从ActionListener 或任何调用代码的地方执行此操作
【解决方案2】:

摄氏转换公式不正确。它是 c = (f - 32) * (5/9)。试试

c = (f - 32) * 5 / 9;

使用你的两个公式,最后你有一个额外的、无与伦比的);删除它们。

【讨论】:

    【解决方案3】:

    double 是原始类型,而Double 是具有方法的包装类。在您的情况下,您正在使用 double 并在其上调用无效的方法。

    因此使用Double

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多