现在,在将此标记为重复之前 - 请听我说。我已经看过其他两个相似的问题。
由于这是一个GUI Java程序,因用户而异,在我的具体情况下,我只是尝试将华氏温度转换为摄氏温度而绝对没有其他功能。
这是我的代码:
package my.tempconvert;
public class TempConvertUI extends javax.swing.JFrame
{
public TempConvertUI()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
Farenheit = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
Celsius = new javax.swing.JTextField();
CalcButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Farenheit");
Farenheit.setText("Insert Farenheit Value");
Farenheit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FarenheitActionPerformed(evt);
}
});
jLabel2.setText("Celsius");
Celsius.setText("Celsius Will Appear Here");
CalcButton.setText("Calculate");
CalcButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CalcButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(Farenheit)
.addComponent(Celsius, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
.addGap(0, 183, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(CalcButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Farenheit, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Celsius, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(CalcButton)
.addGap(0, 205, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void FarenheitActionPerformed(java.awt.event.ActionEvent evt) {
}
private void CalcButtonActionPerformed(java.awt.event.ActionEvent evt) {
float num1, result;
num1 = Float.parseFloat(Farenheit.getText());
result = (((float) 5/9) * (num1 - 32));
Celsius.setText(String.valueOf(result));
}
public static void main(String args[])
{
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TempConvertUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TempConvertUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TempConvertUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TempConvertUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TempConvertUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton CalcButton;
private javax.swing.JTextField Celsius;
private javax.swing.JTextField Farenheit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
没有语法错误 - 只有一个输出错误:
:摄氏度始终输出为0.0或-0.0 - 已解决 -
显然我的代码有问题,我需要帮助才能搞清楚这是什么。
如果有人认为我的问题不明确或者我没有遵循发布问题的指导原则,我会很乐意将这些批评考虑在内,我们都可以继续前进。
答案 0 :(得分:1)
因为(5/9)
的结果被评估为整数,所以它的值为零,因为它只取点的左边的值(例如0.0557 - > 0),你应该把它变成浮动,如下:
num1 = Float.parseFloat(Farenheit.getText());
result = (((float) 5/9) * (num1 - 32));
Celsius.setText(String.valueOf(result));
答案 1 :(得分:1)
result = ((5/9) * (num1 - 32));
通常的整数截断问题。也是多余的括号。
result = 5*(num1-32)/9.0;
答案 2 :(得分:0)
将5/9转换为Float,否则将其评估为整数并自动设置为0.