繁体   English   中英

尝试循环回到起点时陷入无限循环。 爪哇

[英]Stuck in an infinite loop when trying to loop back to the beginning. Java

如果用户输入无效的大小,我试图使代码循环。 但是它陷入了无限循环。 我尝试将“ 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");
               }
    }
}
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的值再次设置为较新的值。

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;

    }

在循环中提示。

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

等等等等等等

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

在while中添加括号。

休息很好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM