2014-11-24 72 views
-1

嘿,我有一個if和else語句,我想循環如果其他發生,因爲這意味着用戶沒有輸入所需的數量,因此程序重新啓動這裏是代碼任何想法?在Java中循環if語句

System.out.println("The area of the glass is " + area); 

    if (thick == 3) 
    { 

     System.out.println("The price to replace the glass will be £" + price1); 
     System.out.println("With VAT the price is £" + Final1 ); 
    } 
    else if (thick == 5) 
    { 

     System.out.println("The price to replace the glass will be £" + price2); 
     System.out.println("With VAT the price is £" + Final2); 
    } 

    else if (thick == 7) 
    { 

    System.out.println("The price to replace the glass will be £" + price3); 
    System.out.println("With VAT the price is £" + Final3); 
    } 
    else 
    { 
    System.out.println("Sorry for the inconvenience but we do not do this size thinkness."); //If the we don't have the thickness this is displayed 
    } 

在此先感謝

+0

你的意思是你是檢查用戶輸入的,如果他們進入它錯誤地,你想重新開始? – Luminusss 2014-11-24 22:31:27

+0

耶基本上如果用戶不輸入3 5或7然後我想程序重新啓動相當新的Java所以任何示例將是一個很大的幫助 – 2014-11-24 22:34:52

+0

剛剛添加了另一個答案,你應該有一些建議現在。 – Luminusss 2014-11-24 22:42:56

回答

2

使用而循環,循環直到變量是可以接受的:

thick = 0; 
while (thick != 3 && thick != 5 && thick != 7) { 
    // read new thick value from user 
    // your current code 
} 

你可以表達上述更整齊地如下:

for (thick = 0; !Arrays.asList(3, 5, 7).contains(thick);) 
    // read new thick value from user 
    // your current code 
} 
+0

這可能是最乾淨的方法之一,你會得到它完成.. – Luminusss 2014-11-24 22:44:01

+0

謝謝你所有我需要的是一個簡單和簡單的任務任務歡呼聲。 – 2014-11-24 22:52:27

0

你可以使用一個循環。如果輸入了有效的答案,請跳出循環。否則,保持循環。

實施例:

while(true){ 
    <get input here> 
    if(thick==3){ 
     <do stuff> 
     break; 
    } 
    else if(...){ 
     <do stuff> 
     break; 
    } 
    ... 
    else{ 
     <do stuff> 
    } 
} 
0
do { 
your code 
}while(the_else_occurs); 

即:

boolean flag=false; 
do{ 
    if (thick == 3) 
    { 

     System.out.println("The price to replace the glass will be £" + price1); 
     System.out.println("With VAT the price is £" + Final1 ); 
    } 
    else if (thick == 5) 
    { 

     System.out.println("The price to replace the glass will be £" + price2); 
     System.out.println("With VAT the price is £" + Final2); 
    } 

    else if (thick == 7) 
    { 

    System.out.println("The price to replace the glass will be £" + price3); 
    System.out.println("With VAT the price is £" + Final3); 
    } 
    else { 
     flag=true; 
    System.out.println("Sorry for the inconvenience but we do not do this size thinkness."); //If the we don't have the thickness this is displayed 
    } 
    }while(flag); 
0

我想你應該在一些輸入驗證拋出:

while(thick != 3 && thick != 5 && thick != 7 && thick != null){ 

    System.out.println("Sorry for the inconvenience but we do not do this size thinkness.");  

    //If the we don't have the thickness this is displayed 
} 
+0

謝謝@ rink.attendant.6應該讓我自己修復它。 – penjoku 2014-11-24 22:45:11

0

您也可以嘗試創建一個方法並回顧它(遞歸),如果沒有正確輸入:

public void MyMethod() { 

System.out.println("The area of the glass is " + area); 

if (thick == 3) 
{ 

    System.out.println("The price to replace the glass will be £" + price1); 
    System.out.println("With VAT the price is £" + Final1 ); 
} 
else if (thick == 5) 
{ 

    System.out.println("The price to replace the glass will be £" + price2); 
    System.out.println("With VAT the price is £" + Final2); 
} 

else if (thick == 7) 
{ 

System.out.println("The price to replace the glass will be £" + price3); 
System.out.println("With VAT the price is £" + Final3); 
} 
else 
{ 
System.out.println("Sorry for the inconvenience but we do not do this size thinkness."); //If the we don't have the thickness this is displayed 
MyMethod(); 
} 
} 

與此唯一的事情是,你必須小心溢出你的堆棧。一個switch聲明可能是一個好主意,但替代。只是一些建議。

0

你可以使用一個散列映射來代替:

HashMap<int, PointF> map = new HashMap<int, PointF>() 
{ 
    { 3, new PointF(4.5f, 6) }, 
    { 5, new PointF(8, 6.3f) }, 
    { 6, new PointF(9, 8) }, 
}; 

if (map.containsKey(thick)) 
{ 
    System.out.println("The price to replace the glass will be £" + map.get(thick).x); 
    System.out.println("With VAT the price is £" + map.get(thick).y); 
} 

,如果你可以用你的自定義類切換的PointF它woulb是更好的方式