在尝试循环回到开头时陷入无限循环。 Java的

时间:2015-11-26 01:48:44

标签: java swing loops infinite-loop joptionpane

如果用户输入无效大小,我试图让代码循环。它虽然陷入无限循环。我尝试将“while”添加到底部并添加“do”以启动循环但是我得到相同的结果或代码停止而没有循环回到开头。

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class BarkingLotJOP5
{
    public static void main(String[] args)
    {
        String size;
        Double sm = 8.00, md = 12.00, lg = 17.00, total, tax = .09;
        DecimalFormat df = new DecimalFormat("$###,###.##");

            JOptionPane.showMessageDialog(null,"Welcome to BarkingLot.com.");
            JOptionPane.showMessageDialog(null, "It is $5 for small, $10 for medium, and $15 for large.");

        size = JOptionPane.showInputDialog("Enter the dog's size.");

        while
            (!size.equals("small") || size.equals("medium")|| size.equals("large"))
                {
        if
            (size.equalsIgnoreCase("small"))
               {
                 total = (sm*tax)+sm;
                 JOptionPane.showMessageDialog(null,"For a small dog, your total is "+ df.format(total)+
                                                                      "\nThank you, have a great day!");
               }
        else
        if
            (size.equalsIgnoreCase("medium"))
               {
                 total = (md*tax)+md;
                 JOptionPane.showMessageDialog(null,"For a medium dog, your total is "+ df.format(total)+
                                                                               "\nThank you, have a great day!");
               }
        else
        if
            (size.equalsIgnoreCase("Large"))
                           {
                             total = (lg*tax)+lg;
                             JOptionPane.showMessageDialog(null,"For a large dog, your total is "+ df.format(total)+
                                                            "\nThank you, have a great day!");
                    }
        else
            JOptionPane.showMessageDialog(null,"Error");
               }
    }
}

3 个答案:

答案 0 :(得分:2)

while(!(size.equals("small") || size.equals("medium")|| size.equals("large")))

(注意大括号)。基本上你的代码检查如下:

在以下情况下继续循环:

尺寸不等于:small

尺寸相等medium

尺寸相等large

但你想要:

在以下情况下继续循环:

尺寸等于(小或中或大)

修改

在else条件中而不是:

else
            JOptionPane.showMessageDialog(null,"Error");

你可以使用:

else{
    JOptionPane.showMessageDialog(null,"Error, try again");
    size = JOptionPane.showInputDialog("Enter the dog's size.");
}

这将使输入框再次出现并再次将size的值设置为更新的值。

答案 1 :(得分:0)

break声明将为您解决问题。

然后移除!

前面的!size.equals("small")符号
    while (size.equals("small") || size.equals("medium") || size.equals("large")) {
        if (size.equalsIgnoreCase("small")) {
            total = (sm * tax) + sm;
            JOptionPane.showMessageDialog(null, "For a small dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        } else if (size.equalsIgnoreCase("medium")) {
            total = (md * tax) + md;
            JOptionPane.showMessageDialog(null, "For a medium dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        } else if (size.equalsIgnoreCase("Large")) {
            total = (lg * tax) + lg;
            JOptionPane.showMessageDialog(null, "For a large dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        } else {
            JOptionPane.showMessageDialog(null, "Error");
            break;
        }
    }

更新:

在这种情况下,我建议您使用switch代替while。对我来说看起来更准确。

    switch (size) {

        case "small":
            total = (sm * tax) + sm;
            JOptionPane.showMessageDialog(null, "For a small dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        case "medium":
            total = (md * tax) + md;
            JOptionPane.showMessageDialog(null, "For a medium dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        case "large":
            total = (lg * tax) + lg;
            JOptionPane.showMessageDialog(null, "For a large dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        default:
            JOptionPane.showMessageDialog(null, "Error");
            break;

    }

答案 2 :(得分:0)

循环提示。

  

do { size = JOptionPane.showInputDialog("Enter the dog's size.");

blah blah blah

}
while (!(size.equals("small") || size.equals("medium") || size.equals("large")))

为while添加括号。

休息很好