2015-11-26 151 views
0

我想讓代碼循環,如果用戶輸入無效的大小。但它陷入了無限循環。我試着將「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"); 
       } 
    } 
} 
+0

您需要檢查循環中的大小,直到它是建議的大小之一。一旦你有一個允許的大小,做其他消息。 – spirographer

回答

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

(注意括號)。基本上你的代碼進行覈對,如:

不斷循環,如果:

大小不等於:small

大小等於到:medium

尺寸爲等於到:large

但是你想:

不斷循環,如果:

大小等於(小型或中型或大型)

編輯:

在其他情況下,而不是:

else 
      JOptionPane.showMessageDialog(null,"Error"); 

你可以使用:

else{ 
    JOptionPane.showMessageDialog(null,"Error, try again"); 
    size = JOptionPane.showInputDialog("Enter the dog's size."); 
} 

這將使得輸入框中再次出現,並重新設置大小的值到新值。

+0

這仍然不會停止無限循環,因爲代碼需要檢查循環內的大小值。目前它不。 – spirographer

+0

對不起,幾乎錯過了它。無論如何,添加代碼來解決這個問題。感謝編輯建議。 –

0

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; 

    } 
+0

你不需要while循環代碼的這一部分 – spirographer

+0

是的。開關就夠了.. :) –

+0

但是''switch'代碼一旦停止'default'的情況下就會停止執行嗎?如果他想反覆迭代,直到用戶輸入正確的值(並且看起來像是他正在嘗試做的那樣),該怎麼辦? –

0

提示在循環中。

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

等等等等

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

添加括號一段時間。

休息很好