我想讓代碼循環,如果用戶輸入無效的大小。但它陷入了無限循環。我試着將「while」帶到底部並添加「do」來啓動循環,但是我得到的結果相同,或者代碼停止循環返回到開頭。嘗試循環回到開始時陷入無限循環。 Java
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");
}
}
}
您需要檢查循環中的大小,直到它是建議的大小之一。一旦你有一個允許的大小,做其他消息。 – spirographer