[英]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.