Java 用于计算温度的if语句和输入
我需要做一个温度转换。当我在华氏度、摄氏度或开尔文中输入一个值,然后按calculate,它将计算其他两个值。只有两个按钮,计算和清除。当我按下计算按钮时,如何输入一个值并让他们计算其他两个值Java 用于计算温度的if语句和输入,java,if-statement,temperature,Java,If Statement,Temperature,我需要做一个温度转换。当我在华氏度、摄氏度或开尔文中输入一个值,然后按calculate,它将计算其他两个值。只有两个按钮,计算和清除。当我按下计算按钮时,如何输入一个值并让他们计算其他两个值 private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { //declare three
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//declare three constant values for absolute zero
double A_ZERO_F = -459.67;
double A_ZERO_C = -273.15;
double A_ZERO_K = 0.0;
// read the data from any of the fields as String types
//We use the JTextField method getText() to do the read
String fahrString = fahrFld.getText();
String celsString = celsFld.getText();
String kelvString = kelvFld.getText();
//convert the Strings into double
double fahrVal = Double.parseDouble(fahrFldg);
double celsVal = Double.parseDouble(celsFld);
double kelvVal = Double.parseDouble(kelvFld);
///need a value to hold the input
double input = 0.0;
//the input entered now needs to be calculated
if(event.equals(fahrString))
{
//fahrenheit to kelvin
fahrVal = ((input-32.0)/1.8) + 273.15;
kelvField.setText("" + fahrValue);
}
else if(event.equals(fahrString))
{
//fahrenheit to celsius
celsVal = (5.0/9.0) * (input-32.0);
celsField.setText(""+ celsVal);
}
else if(event.equals(celsString))
{
//celsius to fahrenheit
fahrVal = ((9.0/5.0)*input) + 32.0;
fahrField.setText(""+ fahrVal);
}
else if(event.equals(celsString))
{
//celsius to kelvin
kelvVal = input + 273.15;
celsField.setText(""+ kelvVal);
}
else if(event.equals(kelvString))
{
//kelvin to fahrenheit
fahrVal = ((input - 273) * 1.8 ) + 32.0;
celsField.setText("" + fahrVal);
}
else if (event.equals(kelvFld))
{
//kelvin to celsius
celsVal = input - 273.15;
celsField.setText(Double.toString(celsVal));
}
//clears all conversions when clear button is pressed
if (event.getSource() == clearButton){
celsFld.setText("");
kelvFld.setText("");
fahrFld.setText("");}
你几乎没有什么解决办法 解决方案1 你可以在某处记住你的旧价值观。在此之后-在按钮处理程序中,您可以将新值与旧值进行比较 然而,像这样带按钮的计算器并不是最好的主意
解决方案2(在我看来-更好)
您可以编写事件,当您更改文本并在该事件处理程序中计算新值时,该事件将在文本框上激发。我不是Java专家,我不能提供代码示例。为您提供的解决方案很少 解决方案1 你可以在某处记住你的旧价值观。在此之后-在按钮处理程序中,您可以将新值与旧值进行比较 然而,像这样带按钮的计算器并不是最好的主意
解决方案2(在我看来-更好)
您可以编写事件,当您更改文本并在该事件处理程序中计算新值时,该事件将在文本框上激发。我不是Java专家,我不能提供代码示例。您的条件有两个主要问题 第一:
if(event.equals(fahrString))
{
// some code
}
else if(event.equals(fahrString))
{
// this will be never be executed, because if the condition
// were true, then the if block would be executed and this skipped
}
// the same for all of your other conditions.
第二:
if(event.equals(fahrString)) {
将始终为
false
,因为事件
是一个动作事件
,fahrString
是一个字符串
你的情况有两个主要问题
第一:
if(event.equals(fahrString))
{
// some code
}
else if(event.equals(fahrString))
{
// this will be never be executed, because if the condition
// were true, then the if block would be executed and this skipped
}
// the same for all of your other conditions.
第二:
if(event.equals(fahrString)) {
将始终为
false
,因为事件
是一个动作事件
,fahrString
是一个字符串
你可以试试这样的东西。基本上,每次编辑一个字段并保存编辑的实例时,MyKeyAdapter都会运行。接下来,检查编辑了哪个字段并启动计算。clearButton应该在单独的侦听器中处理,因为在这种情况下不需要计算温度。希望你能得到这个想法,并且你能进步
private void initStuff () {
calculateButton.addActionListener(new ButtonActionEvent());
clearButton.addActionListener(new ButtonActionEvent());
fahrFld.addKeyListener(new MyKeyAdapter());
celsFld.addKeyListener(new MyKeyAdapter());
kelvFld.addKeyListener(new MyKeyAdapter());
}
private JTextField lastModified;
private class MyKeyAdapter extends KeyAdapter {
@Override
public void keyTyped(KeyEvent e) {
lastModified = (JTextField)e.getSource();
}
}
private class ButtonActionEvent implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//declare three constant values for absolute zero
double A_ZERO_F = -459.67;
double A_ZERO_C = -273.15;
double A_ZERO_K = 0.0;
if(lastModified == fahrFld)
{
double input = Double.parseDouble(fahrFld.getText());
//fahrenheit to kelvin
double kelvVal = ((input-32.0)/1.8) + 273.15;
kelvFld.setText("" + kelvVal);
//fahrenheit to celsius
double celsVal = (5.0/9.0) * (input-32.0);
celsFld.setText(""+ celsVal);
}
else if(lastModified == celsFld)
{
double input = Double.parseDouble(celsFld.getText());
//celsius to fahrenheit
double fahrVal = ((9.0/5.0)*input) + 32.0;
fahrFld.setText(""+ fahrVal);
//celsius to kelvin
double kelvVal = input + 273.15;
kelvFld.setText(""+ kelvVal);
}
else if(lastModified == kelvFld)
{
double input = Double.parseDouble(kelvFld.getText());
//kelvin to fahrenheit
double fahrVal = ((input - 273) * 1.8 ) + 32.0;
fahrFld.setText("" + fahrVal);
//kelvin to celsius
double celsVal = input - 273.15;
celsFld.setText(Double.toString(celsVal));
} else {
return;
}
//clears all conversions when clear button is pressed
if (event.getSource() == clearButton){
celsFld.setText("");
kelvFld.setText("");
fahrFld.setText("");
}
}
}
你可以试试这样的。基本上,每次编辑一个字段并保存编辑的实例时,MyKeyAdapter都会运行。接下来,检查编辑了哪个字段并启动计算。clearButton应该在单独的侦听器中处理,因为在这种情况下不需要计算温度。希望你能得到这个想法,并且你能进步
private void initStuff () {
calculateButton.addActionListener(new ButtonActionEvent());
clearButton.addActionListener(new ButtonActionEvent());
fahrFld.addKeyListener(new MyKeyAdapter());
celsFld.addKeyListener(new MyKeyAdapter());
kelvFld.addKeyListener(new MyKeyAdapter());
}
private JTextField lastModified;
private class MyKeyAdapter extends KeyAdapter {
@Override
public void keyTyped(KeyEvent e) {
lastModified = (JTextField)e.getSource();
}
}
private class ButtonActionEvent implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//declare three constant values for absolute zero
double A_ZERO_F = -459.67;
double A_ZERO_C = -273.15;
double A_ZERO_K = 0.0;
if(lastModified == fahrFld)
{
double input = Double.parseDouble(fahrFld.getText());
//fahrenheit to kelvin
double kelvVal = ((input-32.0)/1.8) + 273.15;
kelvFld.setText("" + kelvVal);
//fahrenheit to celsius
double celsVal = (5.0/9.0) * (input-32.0);
celsFld.setText(""+ celsVal);
}
else if(lastModified == celsFld)
{
double input = Double.parseDouble(celsFld.getText());
//celsius to fahrenheit
double fahrVal = ((9.0/5.0)*input) + 32.0;
fahrFld.setText(""+ fahrVal);
//celsius to kelvin
double kelvVal = input + 273.15;
kelvFld.setText(""+ kelvVal);
}
else if(lastModified == kelvFld)
{
double input = Double.parseDouble(kelvFld.getText());
//kelvin to fahrenheit
double fahrVal = ((input - 273) * 1.8 ) + 32.0;
fahrFld.setText("" + fahrVal);
//kelvin to celsius
double celsVal = input - 273.15;
celsFld.setText(Double.toString(celsVal));
} else {
return;
}
//clears all conversions when clear button is pressed
if (event.getSource() == clearButton){
celsFld.setText("");
kelvFld.setText("");
fahrFld.setText("");
}
}
}
将0用作所有3的数据集。哪个字段的
值为非0
使用它来计算其余值。如果全部为0,则简单。如果不止一个是非零的,则选择您喜欢用作基准的选项。如果event.equals(fahrString)
为真,则else If event.equals(fahrString)
将永远不会执行,因为已执行原始条件。这与celsString
相同。您应该将两个条件中的所有代码组合到一个块中。@JakeWilson:event.equals(fahrString)
永远不会是true
,因为event
是一个ActionEvent
而fahrString
是一个字符串
@jlordo:你说得对;“我想你当时发现了他的问题。”rocketboy,这可能不是一个好主意,因为询问华氏0摄氏度是完全合理的,反之亦然。如果这三个都是0,那么你就不知道用户真正想用哪一个作为基数。使用0作为所有3的deafults。哪个字段的值为非0
使用它来计算其余值。如果全部为0,则简单。如果不止一个是非零的,则选择您喜欢用作基准的选项。如果event.equals(fahrString)
为真,则else If event.equals(fahrString)
将永远不会执行,因为已执行原始条件。这与celsString
相同。您应该将两个条件中的所有代码组合到一个块中。@JakeWilson:event.equals(fahrString)
永远不会是true
,因为event
是一个ActionEvent
而fahrString
是一个字符串
@jlordo:你说得对;“我想你当时发现了他的问题。”rocketboy,这可能不是一个好主意,因为询问华氏0摄氏度是完全合理的,反之亦然。如果这三个都是0,那么你就不知道用户真正想用哪一个作为基础。哦,好吧,我明白了问题所在。我只是不知道如何只输入一个值,然后让它计算其他两个值。我将if改为if(event.getSource()==calcButton)
哦,好的,我看到问题了。我只是不知道如何只输入一个值,然后让它计算其他两个值。我将if改为if(event.getSource()==calcButton)