2014-07-10 45 views
-2

這是我班上的作業。這是一個自動訂購系統。我還是Java新手,所以一切都不一定點擊。我認爲大多數情況下大部分都有效,但我遇到的主要問題是製作循環本身並退出。如何爲我的循環做出退出?

import java.util.Scanner; 
public class Metal { 
public static void main (String[] args) { 
    double PRICE1 = 5.00; 
    double PRICE2 = 7.00; 
    double PRICE3 = 3.50; 
    double PRICE4 = 0.75; 
    double TAX = 0.05; 

    System.out.println ("Metal Down Your Mouth Menu"); 
    System.out.println(); 
    System.out.println(); 
    System.out.println(); 
    System.out.println ("1. Black Sabbath Burgers (Hamburgers With Black Buns) " + PRICE1); 
    System.out.println ("2. Rack of Lamb of God (Rack of Lamb) " + PRICE2); 
    System.out.println ("3. Texas Hippie Collar Greens (Collar Greens) " + PRICE3); 
    System.out.println ("4. Pepsi " + PRICE4); 
    System.out.println ("Press any other button to stop ordering."); 


    Scanner userInput = new Scanner(System.in); 
    int itemnumber = 0; 
    while (itemnumber < 1 || itemnumber > 4) { 
    System.out.print("Enter the item number of the item you wish to order: "); 
    itemnumber = userInput.nextInt(); 
    } 

    System.out.print ("How many?"); 
    int amount = userInput.nextInt(); 

     double subtotal = 0; 
     double total = 0; 
     double price = 0; 
     double taxes = 0; 
     String name = ""; 

    switch (itemnumber){ 
     case 1: name = "Black Sabbath Burgers"; price = PRICE1; break; 
     case 2: name = "Rack of Lamb of God"; price = PRICE2; break; 
     case 3: name = "Texas Hippie Collar Greens"; price = PRICE3; break; 
     case 4: name = "Pepsi"; price = PRICE4; break; 
    } 
     subtotal = price * amount; 
     total = subtotal + total; 
     System.out.print("Price for items: " + subtotal); 
     System.out.print("Price Total: " + total); 
} 
+0

您的循環條件反轉。 – Luaan

+0

這開始幫助我,但我無法讓它循環。 –

+0

正確的循環?如果你想重複這個菜單,你很早就關閉了循環體。當然,你會想要使用'do ... while'循環,否則'itemnumber'爲零,因此在第一次讀取值之前進行檢查。 – Luaan

回答

1

這是我第一次在這個網站發帖,但我想我找到了你的問題。有兩個大的誤差,由箭頭指示:

while (itemnumber >= 1 || <-- itemnumber <= 4) { 
System.out.print("Enter the item number of the item you wish to order: "); 
itemnumber = userInput.nextInt(); 
} <-- 

1)這應該是一個「& &」不是「||」。你希望它在範圍內。現在它讀取的數字必須大於或等於1或小於或等於4,這些都是整數。

2)你提前關閉你的循環。你的代碼現在做什麼(在& &開關後面)是需要數字1-4,並不斷重複「輸入項目編號....」行,直到你把一個數字不在範圍內,然後繼續。

修復:有幾種方法可以解決這個問題。我的解決將是這樣,和之後的解釋會:

import java.util.Scanner; 
public class Metal { 
public static void main (String[] args) { 
double PRICE1 = 5.00; 
double PRICE2 = 7.00; 
double PRICE3 = 3.50; 
double PRICE4 = 0.75; 
double TAX = 0.05; 

System.out.println ("Metal Down Your Mouth Menu"); 
System.out.println(); 
System.out.println(); 
System.out.println(); 
System.out.println ("1. Black Sabbath Burgers (Hamburgers With Black Buns) " + PRICE1); 
System.out.println ("2. Rack of Lamb of God (Rack of Lamb) " + PRICE2); 
System.out.println ("3. Texas Hippie Collar Greens (Collar Greens) " + PRICE3); 
System.out.println ("4. Pepsi " + PRICE4); 
System.out.println ("Press any other button to stop ordering."); 


Scanner userInput = new Scanner(System.in); 
int itemnumber = 0; 
System.out.print("Enter the item number of the item you wish to order: "); 
itemnumber = userInput.nextInt(); 
double total = 0; 
while (itemnumber >= 1 && itemnumber <= 4) { 


System.out.print ("How many?"); 
int amount = userInput.nextInt(); 

    double subtotal = 0; 
    double price = 0; 
    double taxes = 0; 
    String name = ""; 

switch (itemnumber) 
{ 
    case 1: name = "Black Sabbath Burgers"; price = PRICE1; break; 
    case 2: name = "Rack of Lamb of God"; price = PRICE2; break; 
    case 3: name = "Texas Hippie Collar Greens"; price = PRICE3; break; 
    case 4: name = "Pepsi"; price = PRICE4; break; 
} 
    subtotal = price * amount; 
    total = subtotal + total; 
    System.out.print("Price for items: " + subtotal); 
    System.out.print("Enter the item number of the item you wish to order: "); 
    itemnumber = userInput.nextInt(); 
} 
System.out.print("Price Total: " + total);   
} 
} 

解釋:從本質上說,你有喜歡它的90%。我將提到的'}'移到了這裏的末尾:

 itemnumber = userInput.nextInt(); 
    } <-- 

這樣,它循環遍歷此代碼,直到用戶結束。 此外,您的循環不需要太多修復。它可以與修補程序& &一起使用。但是,您必須在循環之前放置頂部的行。

System.out.print("Enter the item number of the item you wish to order: "); 
itemnumber = userInput.nextInt(); 

然後你在循環的最後放置相同的行來重置itemnumber。你的循環做的是如果itemnumber在1和4之間,它執行下面的代碼。否則,它停止。通過在進入循環之前進行檢查,您可以設置itemnumber,以便循環可以檢查某些內容。並且你把下一個輸入放在循環結尾,這樣你的程序在完成第一次執行之前就完成了,然後再進入下一個。

另外,您應該將變量'total'移出循環,如上所示。如果你持續循環並將其重置爲0,那麼每次你的總數都會輸出0。最好保留循環內部的全部內容,並在循環內進行修改。

小提示,使用System.out.println();而不是System.out.print();它將輸出放在自己的路線上。看起來好一點。

我認爲這涵蓋了它。如果你想要更多的解釋,我會很樂意把它給你。一旦習慣了Java,Java很有趣。這只是需要時間。 :D

+0

謝謝!這有幫助! –