我无法获得更改 if 语句的按钮
public class calcButtonHandler3 implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
DecimalFormat num3 = new DecimalFormat(",###.##");
double feet, milli, meters, yards, inches;
String str1 = tFeet.getText();
String str2 = tYards.getText();
String str3 = tInches.getText();
String str4 = tMeters.getText();
String str5 = tMilli.getText();
if (str1 != "")
{
feet = Double.parseDouble(tFeet.getText());
yards = feet / 3;
inches = feet * 12;
milli = feet * 304.8;
meters = feet * 0.3048;
tYards.setText(num3.format(yards));
tInches.setText(num3.format(inches));
tMilli.setText(num3.format(milli));
tMeters.setText(num3.format(meters));
}
else if (str2 != "")
{
yards = Double.parseDouble(tYards.getText());
feet = yards * 3;
inches = yards * 36;
milli = yards * 914.4;
meters = yards * 0.9144;
tFeet.setText(num3.format(feet));
tInches.setText(num3.format(inches));
tMilli.setText(num3.format(milli));
tMeters.setText(num3.format(meters));
}
else if (str3 != "")
{
inches = Double.parseDouble(tInches.getText());
feet = inches * 3;
yards = inches * 36;
milli = inches * 25.4;
meters = inches * 0.0254;
tFeet.setText(num3.format(feet));
tYards.setText(num3.format(yards));
tMilli.setText(num3.format(milli));
tMeters.setText(num3.format(meters));
}
else if (str4 != "")
{
meters = Double.parseDouble(tMeters.getText());
feet = meters * 3.2808399;
inches = meters * 39.37007874;
milli = meters * 1000 ;
yards = meters * 1.0936133;
tFeet.setText(num3.format(feet));
tInches.setText(num3.format(inches));
tMilli.setText(num3.format(milli));
tYards.setText(num3.format(yards));
}
else if (str5 != "")
{
milli = Double.parseDouble(tMilli.getText());
feet = milli * 0.00328084;
inches = milli * 0.03937008;
yards = milli * 0.00109361;
meters = milli / 1000;
tFeet.setText(num3.format(feet));
tInches.setText(num3.format(inches));
tYards.setText(num3.format(yards));
tMeters.setText(num3.format(meters));
}
else
{
JOptionPane.showInputDialog("Please input a number");
}
}
}
如何获得切换到其他 if 语句的按钮?当我运行此代码时,如果 tFeet 字段中没有任何内容,则什么也不会发生。
public class calcButtonHandler3 implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
DecimalFormat num3 = new DecimalFormat(",###.##");
double feet, milli, meters, yards, inches;
String str1 = tFeet.getText();
String str2 = tYards.getText();
String str3 = tInches.getText();
String str4 = tMeters.getText();
String str5 = tMilli.getText();
if (str1 != "")
{
feet = Double.parseDouble(tFeet.getText());
yards = feet / 3;
inches = feet * 12;
milli = feet * 304.8;
meters = feet * 0.3048;
tYards.setText(num3.format(yards));
tInches.setText(num3.format(inches));
tMilli.setText(num3.format(milli));
tMeters.setText(num3.format(meters));
}
else if (str2 != "")
{
yards = Double.parseDouble(tYards.getText());
feet = yards * 3;
inches = yards * 36;
milli = yards * 914.4;
meters = yards * 0.9144;
tFeet.setText(num3.format(feet));
tInches.setText(num3.format(inches));
tMilli.setText(num3.format(milli));
tMeters.setText(num3.format(meters));
}
else if (str3 != "")
{
inches = Double.parseDouble(tInches.getText());
feet = inches * 3;
yards = inches * 36;
milli = inches * 25.4;
meters = inches * 0.0254;
tFeet.setText(num3.format(feet));
tYards.setText(num3.format(yards));
tMilli.setText(num3.format(milli));
tMeters.setText(num3.format(meters));
}
else if (str4 != "")
{
meters = Double.parseDouble(tMeters.getText());
feet = meters * 3.2808399;
inches = meters * 39.37007874;
milli = meters * 1000 ;
yards = meters * 1.0936133;
tFeet.setText(num3.format(feet));
tInches.setText(num3.format(inches));
tMilli.setText(num3.format(milli));
tYards.setText(num3.format(yards));
}
else if (str5 != "")
{
milli = Double.parseDouble(tMilli.getText());
feet = milli * 0.00328084;
inches = milli * 0.03937008;
yards = milli * 0.00109361;
meters = milli / 1000;
tFeet.setText(num3.format(feet));
tInches.setText(num3.format(inches));
tYards.setText(num3.format(yards));
tMeters.setText(num3.format(meters));
}
else
{
JOptionPane.showInputDialog("Please input a number");
}
}
}
How can I get the button to switch to the other if statements? when I run this code if nothing is in the tFeet Field, then nothing happens.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的条件并不相互排斥。
如果您需要执行所有这些语句,那么这些语句应该是五个单独的
if
语句,而不是一个if...else if
语句。另外,您应该使用 String
equals
方法来比较 String 值。Your conditions are not mutually exclusive.
Those should be five separate
if
statements instead of oneif...else if
statement if you need all of them to execute.Also, you should be using the String
equals
method to compare String values.