java编程总提示输入为空,貌似else if后面指令没有运行
importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjava.math.*;publicc...
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.math.*;
public class TheSecExp extends Applet implements ActionListener{
Frame frame;
Label lbCentigrade;
Label lbFahrenheit;
TextField txtCentigrade;
TextField txtFahrenheit;
Button btConversion;
public void init(){
lbCentigrade=new Label("摄氏度:");
lbFahrenheit=new Label("华氏度:");
txtCentigrade=new TextField(20);
txtFahrenheit=new TextField(20);
btConversion=new Button("转换");
add(lbCentigrade);
add(txtCentigrade);
add(lbFahrenheit);
add(txtFahrenheit);
add(btConversion);
btConversion.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String centigrade=txtCentigrade.getText();
String fahrenheit=txtFahrenheit.getText();
if(txtCentigrade.getText()!=null){
double cen1=Double.parseDouble(centigrade);
double fah1=cen1*1.8+32;
String f=Double.toString(fah1);
txtFahrenheit.setText(f);
}
else if(txtFahrenheit.getText()!=null){
double fah2=Double.parseDouble(fahrenheit);
double cen2=(fah2-32)/1.8;
String c=Double.toString(cen2);
txtCentigrade.setText(c);
}
}
} 展开
import java.awt.event.*;
import java.applet.*;
import java.math.*;
public class TheSecExp extends Applet implements ActionListener{
Frame frame;
Label lbCentigrade;
Label lbFahrenheit;
TextField txtCentigrade;
TextField txtFahrenheit;
Button btConversion;
public void init(){
lbCentigrade=new Label("摄氏度:");
lbFahrenheit=new Label("华氏度:");
txtCentigrade=new TextField(20);
txtFahrenheit=new TextField(20);
btConversion=new Button("转换");
add(lbCentigrade);
add(txtCentigrade);
add(lbFahrenheit);
add(txtFahrenheit);
add(btConversion);
btConversion.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String centigrade=txtCentigrade.getText();
String fahrenheit=txtFahrenheit.getText();
if(txtCentigrade.getText()!=null){
double cen1=Double.parseDouble(centigrade);
double fah1=cen1*1.8+32;
String f=Double.toString(fah1);
txtFahrenheit.setText(f);
}
else if(txtFahrenheit.getText()!=null){
double fah2=Double.parseDouble(fahrenheit);
double cen2=(fah2-32)/1.8;
String c=Double.toString(cen2);
txtCentigrade.setText(c);
}
}
} 展开
1个回答
展开全部
String centigrade = txtCentigrade.getText().trim();
String fahrenheit = txtFahrenheit.getText().trim();
if(!"".equals(centigrade))
{
double cen1 = Double.parseDouble(centigrade);
double fah1 = cen1 * 1.8 + 32;
String f = Double.toString(fah1);
txtFahrenheit.setText(f);
}
else if(!"".equals(fahrenheit))
{
double fah2 = Double.parseDouble(fahrenheit);
double cen2 = (fah2 - 32) / 1.8;
String c = Double.toString(cen2);
txtCentigrade.setText(c);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询