2015-09-01 73 views
2

此代碼應該顯示一個菜單,之後它應該給用戶從菜單中選擇的可能性。用戶可以隨着數量選擇3個項目,之後顯示總價格並停止程序。 這是程序應該是什麼在運行時,如:Java菜單使用Do while循環不斷重複

This is the menu for Tal'Qroq Restourant: 


This is the menu of pizzas 

A.Margherita ..... $5.50 
B.Capricosa ..... $6.50 
C.Funghi ..... $7.00 
D.Vegeterian..... $7.00 
E.Tropical..... $8.00 
F.Meat ..... $8.00 
G.Salami..... $8.00 
H.Maltija..... $8.00 
I.Calzona..... $8.50 
J.Tal'Qroq special..... $8.00 

Enter your pizza order according to the menu letters: 

用戶後輸入比薩餅的數量要求,並且工作正常。 用戶必須被要求3次才能進入披薩和數量,但是循環不會停下來並不停地詢問和詢問,這是我的問題! 以下是代碼:

public class Menu{ 
    public static void main (String[]args){ 

    float total = 0; 
    char cas = 0; 
    int quant = 0; 
    int count = 0; 

    System.out.println("This is the menu for Tal'Qroq Restourant:"); 
    System.out.println("\n"); 
    System.out.println("This is the menu of pizzas"); 
    System.out.println("\n"); 
    System.out.println("A.Margherita ..... $5.50"); 
    System.out.println("\n"); 
    System.out.println("B.Capricosa ..... $6.50"); 
    System.out.println("\n"); 
    System.out.println("C.Funghi ..... $7.00"); 
    System.out.println("\n"); 
    System.out.println("D.Vegeterian..... $7.00"); 
    System.out.println("\n"); 
    System.out.println("E.Tropical..... $8.00"); 
    System.out.println("\n"); 
    System.out.println("F.Meat ..... $8.00"); 
    System.out.println("\n"); 
    System.out.println("G.Salami..... $8.00"); 
    System.out.println("\n"); 
    System.out.println("H.Maltija..... $8.00"); 
    System.out.println("\n"); 
    System.out.println("I.Calzona..... $8.50"); 
    System.out.println("\n"); 
    System.out.println("J.Tal'Qroq special..... $8.00"); 
    System.out.println("\n"); 

    float a = 5.50f; 
    float b = 6.50f; 
    float c = 7.00f; 
    float d = 7.00f; 
    float e = 8.00f; 
    float f = 8.00f; 
    float g = 8.00f; 
    float h = 8.00f; 
    float i = 8.00f; 
    float j = 8.00f; 


    do{ 
     System.out.print("Enter your pizza order according to the menu letters: "); 
     cas = Keyboard.readChar(); 

     System.out.print("Enter the ammount of pizza you want: "); 
     quant = Keyboard.readInt(); 

     if(cas == 'a' || cas == 'A'){ 
     System.out.println("Total for " + quant + " Margherita is :" + (a*quant)); 
     System.out.println("\n"); 
     total = total + (a*quant); 
     count = count++; 

     }else if(cas == 'b' || cas == 'B'){ 
      System.out.println("Total for " + quant + " Capricosa is :" + (b*quant)); 
      System.out.println("\n"); 
      total = total + (b*quant); 
      count = count++; 

     }else if(cas == 'c' || cas == 'C'){ 
      System.out.println("Total for " + quant + " Funghi is :" + (c*quant)); 
      System.out.println("\n"); 
      total = total + (c*quant); 
      count = count++; 

     }else if(cas == 'd' || cas == 'D'){ 
      System.out.println("Total for " + quant + " Vegeterian is :" + (d*quant)); 
      System.out.println("\n"); 
      total = total + (d*quant); 
      count = count++; 

     }else if(cas == 'e' || cas == 'E'){ 
      System.out.println("Total for " + quant + " Tropical is :" + (e*quant)); 
      System.out.println("\n"); 
      total = total + (e*quant); 
      count = count++; 

     }else if(cas == 'f' || cas == 'F'){ 
      System.out.println("Total for " + quant + " Meat is :" + (f*quant)); 
      System.out.println("\n"); 
      total = total + (f*quant); 
      count = count++; 

     }else if(cas == 'g' || cas == 'G'){ 
      System.out.println("Total for " + quant + " Salami is :" + (g*quant)); 
      System.out.println("\n"); 
      total = total + (g*quant); 
      count = count++; 

     }else if(cas == 'h' || cas == 'H'){ 
      System.out.println("Total for " + quant + " Calzona is :" + (h*quant)); 
      System.out.println("\n"); 
      total = total + (h*quant); 
      count = count++; 

     }else if(cas == 'i' || cas == 'I'){ 
      System.out.println("Total for " + quant + " Maltija is :" + (i*quant)); 
      System.out.println("\n"); 
      total = total + (i*quant); 
      count = count++; 

     }else if(cas == 'j' || cas == 'J'){ 
      System.out.println("Total for " + quant + " Tal'Qroq special is :" + (j*quant)); 
      System.out.println("\n"); 
      total = total + (j*quant); 
      count = count++; 
     }else{ 
     System.out.println("Your selection isn't avaliable in our Menu!"); 
     System.out.println("\n"); 
    }  
    } while (count <= 3); 
    System.out.println("Your total is €" + total); 
} 

}

任何答案,或幫助高度讚賞:)。

+0

你提供什麼輸入?計數似乎並不總是增加。使count ++語句成爲while塊的最後一個語句(不要在任何地方重複它),並確保它適用於所有可能性編輯 - 此外,在這裏使用switch case語句而不是if-else結構。它會讓你的代碼更容易閱讀 – Stultuske

+0

只是一個標記:你在條件中的字符串比較是錯誤的,使用equals()和toLowerCase()來避免OR條件。 – drgPP

+0

用++計算 –

回答

3

您的陳述count = count++看起來不對。僅使用count++count = count + 1

使用count = count++創建字節碼有點像這樣:

​​

所以實際上計數沒有得到增加。

使用count = ++count應該工作,但在邏輯上看起來錯誤:

count = count + 1 
count = count 

最好不要嘗試編譯器的有趣的特性

+0

認爲你釘了它。在臨時值(未存儲)之前分配的值被設置。嗯,錯過了。 – Stultuske

+0

或'count = ++ count;''count ++;'是最好的選擇。 –

0
int i = 1; 
i = i++; 
System.out.prinln(i); //-> 1 

您只需編寫「i ++;」然後你很好。

而順便說一句。從來沒有存錢的浮動或雙)

編輯:

int i = 1; 
i = ++i; 

,因爲你是我增加分配新值之前這將是可能的。

0

你有幾件事情可以在你的程序中改進。首先,讓我們來看看這個bug,你說的是count = count ++,但是通過這樣做,編譯器將開始遞增計數值,然後將計數恢復到它的舊值,因爲count++將返回不是遞增值。爲了簡化,這個聲明什麼也不做,也不增加count的值。

當你想增加,使用後綴操作符: count++;

通過分析你的程序,你可以注意到很多重複的代碼可以減少!

例如,比較輸入時,您可以使用.equals(),這樣你就不必爲大寫和小寫分裂的情況:

(cas == 'f' || cas == 'F') 

相當於

cas.equals('f') 

您可以重複其他情況。

在每個if語句中,您正在執行相同的system.out.println。爲了簡單起見,你可以聲明打印一個變量,像

String typeofPizza; 

,然後將每個if

typeofPizza = "Margherita"; 

這樣裏面,可以取出打印到if條款的結束:

System.out.println("Total for " + quant + typeofPizza + "is :" + total); 
+0

'(cas =='f'|| cas​​ =='F')'**不等於'cas.equals('f')'!你可能的意思是:'cas.equalsIgnoreCase('f')',不是嗎? – bobbel

0

感謝您的早期迴應:)。程序運行良好。我無法相信一個犯這種低級錯誤count ++而不是++ count。我也發生了變化詮釋計數到而不是