我无法获得更改 if 语句的按钮

发布于 2024-12-01 08:59:11 字数 2853 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2024-12-08 08:59:11

您的条件并不相互排斥。

if (str1 != "")...  
else if (str2 != "")...  
else if (str3 != "")...  
else if (str4 != "")...  
else if (str5 != "")...  

如果您需要执行所有这些语句,那么这些语句应该是五个单独的 if 语句,而不是一个 if...else if 语句。

另外,您应该使用 String equals 方法来比较 String 值。

if (!str1.equals(""))...  

Your conditions are not mutually exclusive.

if (str1 != "")...  
else if (str2 != "")...  
else if (str3 != "")...  
else if (str4 != "")...  
else if (str5 != "")...  

Those should be five separate if statements instead of one if...else if statement if you need all of them to execute.

Also, you should be using the String equals method to compare String values.

if (!str1.equals(""))...  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文