【问题标题】:Stuck in an infinite loop when trying to loop back to the beginning. Java尝试循环回到开头时陷入无限循环。爪哇
【发布时间】:2015-11-26 01:48:44
【问题描述】:

如果用户输入的尺寸无效,我会尝试让代码循环。虽然它陷入了无限循环。我尝试将“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");
               }
    }
}

【问题讨论】:

  • 您需要检查循环中的尺寸,直到它是推荐的尺寸之一。获得允许的尺寸之一后,请执行其他消息传递。

标签: java swing loops infinite-loop joptionpane


【解决方案1】:
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.");
}

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

【讨论】:

  • 这仍然不会停止无限循环,因为代码需要检查循环内的大小值。目前没有。
  • 抱歉那部分,差点错过。无论如何,添加代码来解决这个问题。感谢您的编辑建议。
【解决方案2】:

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;

    }

【讨论】:

  • 这部分代码不需要while循环
  • 是的。开关就足够了.. :)
  • 但是switch 代码在default 案例执行后不会停止吗?如果他想一次又一次地迭代直到用户输入正确的值(看起来这就是他想要做的)怎么办?
【解决方案3】:

在循环中提示。

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

哈哈哈哈哈哈

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

给 while 添加括号。

休息好了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2018-05-24
    • 1970-01-01
    • 2020-05-13
    相关资源
    最近更新 更多