// 我正在尝试使用 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