【发布时间】: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