简体   繁体   English

温度转换在JFrame中无法正常工作

[英]Temperature Conversion not working properly in JFrame

I can not seem to figure out what I am doing wrong in my Event Handler; 我似乎无法弄清楚我在事件处理程序中做错了什么。 and I am not a good programmer. 而且我不是一个好的程序员。 I can convert from Celsius to Fahrenheit but not the other way around. 我可以将摄氏温度转换为华氏温度,反之则不能。 It usually shows 0.0 for Fahrenheit to Celsius conversions. 对于华氏度到摄氏度的转换,通常显示为0.0。 I think it might have something to do with cfList which is my JComboBox to choose Celsius or Fahrenheit. 我认为这可能与cfList有关 ,这是我的JComboBox以选择摄氏度或华氏度。

Also I want to be able to click the convert button without putting in a value, and the JTextField will automatically be set to zero and show a message that says "Value set to zero." 另外,我希望能够在不输入值的情况下单击转换按钮,并且JTextField将自动设置为零,并显示一条消息,指出“值设置为零”。

So how can I get this whole thing to work: 因此,我如何才能使整个工作正常进行:

  • Convert Celsius to Fahrenheit 转换摄氏到华氏度
  • Convert Fahrenheit to Celsius 转换华氏到摄氏
  • Convert Celsius to Fahrenheit without a value(set to zero) 将摄氏温度转换为华氏度而没有值(设置为零)
  • Convert Fahrenheit to Celsius without a value(set to zero) 无值将华氏温度转换为摄氏温度(设置为零)

     class TCalcButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { double far, cel; String ff, cc; String inString; inString = tempField.getText(); if(inString==null && cfList.getSelectedIndex() == 0) { degree.setText("F"); cel=0; far = cel*(9/5)+32; ff = far+""; resultField.setText(ff); tempSysField.setText("Value set to zero"); } if(inString==null && cfList.getSelectedIndex() == 1) { degree.setText("C"); far=0; cel = (5/9)*(far-32); cc = cel+""; resultField.setText(cc); tempSysField.setText("Value set to zero"); } if(cfList.getSelectedIndex()==0 && inString!=null) { degree.setText("F"); cel = Double.parseDouble(inString); far = cel*(9/5)+32; ff = far+""; resultField.setText(ff); tempSysField.setText(""); } if(cfList.getSelectedIndex()==1 && inString!=null) { degree.setText("C"); far = Double.parseDouble(inString); cel = (far-32)*(5/9); cc = cel+""; resultField.setText(cc); tempSysField.setText(""); } } } 

Your question title states: 您的问题标题指出:

Temperature Conversion not working properly ... 温度转换无法正常工作...

Almost always this is due to your doing int division unknowingly, and in fact, that's exactly what is going on: 几乎总是这是由于您在不知情的情况下执行int除法,实际上,这正是发生的情况:

far = cel*(9/5)+32;

Change 9/5 to 9.0/5.0 将9/5更改为9.0 / 5.0

far = cel * (9.0 / 5.0) + 32;

Why is this important? 为什么这很重要? int division (dividing one int by another) always returns an int, with the fractional part truncated away. int除法(将一个int除以另一个int)总是返回一个int,小数部分被截断。 So 9/5 returns 1 and 5/9 returns 0. If you want the calculation to retain the fractional portion, then at least one double has to be part of the division equation, so in fact 9.0 / 5 would work just fine as would ((double)9/5) 因此9/5返回1,而5/9返回0。如果要让计算保留小数部分,则除法方程式必须至少包含一个双精度数,因此实际上9.0 / 5可以正常工作((double)9/5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM