2017-04-14 49 views
0

我在執行總帳單顯示時遇到了一些麻煩。我問我的教授是誰給我的代碼,這個小塊,這是我修改,以滿足我的需求,但它不執行和某些原因,有錯誤:顯示帳單總額,Java公共無效顯示()錯誤

Illegal start to expression for line --> public void display().

編譯器還建議用半結束我不相信的結腸是準確的。

我錯過了什麼,public void display()沒有被執行,是錯誤的?

import java.util.Scanner; 

public class CoffeeShop 

{ 
/* Author: 
    Date: 
    Program: Create a Coffee Shop application that uses a while loop to build a customer order. The Coffee Shops sells coffee ($3.25), espresso ($4.25), and tea ($2.75). The coffee selection presents the customer with the choices of iced (no charge), cream (50 cents), and sugar (50 cents). The espresso selection presents the customer with choice of caramel (no charge) and chocolate (no charge) with one shot (no charge) or two shots ($1.25) of espresso. Once the customer is done, he or she will receive a bill of the total price. After each selection, the customer will have the choice of returning to the main menu for additional purchases. Use nested loops to handle customers' submenu selections. 
*/ 
    public static void main(String[] args) 

    { 

    //declarations 
     double coff = 3.25; 
     double esp = 4.25; 
     double tea = 2.75; 
     double cream = .50; 
     double sugar = .50; 
     double dblShot = 1.25; 
     int dblshotQty = 0; 
     int userInput = 0; 
     int userInput2 = 0; 
     int coffQty = 0; 
     int espQty = 0; 
     int teaQty = 0; 
     int creamQty = 0; 
     int sugarQty = 0; 
     double runTotal = 0; 
     double totalCoff = 0; 
     double totalEsp = 0; 
     double totalTea = 0; 
     double totalBill = 0; 






     Scanner scan = new Scanner(System.in); 

     System.out.print("Would you like to place an order? press 1 for yes or 2 for no :"); 
     //start a loop with a control variable asking if they would like a cup of coffee yes or no 
     userInput = scan.nextInt(); 
     while(userInput == 1) 
     { 

     System.out.print("Enter 1 for Coffee, 2 for Espresso, or 3 for tea: "); 

     userInput2 = scan.nextInt(); 
     switch(userInput2) 
     //if 1 is pressed coffee is ordered 
     { // open switch 
      case '1': 
       { 
        coffQty = coffQty + 1; 
        System.out.print("Press 1 if you would like your coffee iced or 2 for no: "); 
        userInput = scan.nextInt(); 
       } 
       { 
        System.out.print("Press 1 if you would like cream for $.50 or 2 for no: "); 
        userInput = scan.nextInt(); 
        if (userInput == 1) 
        { 
        creamQty = creamQty + 1; 
        } 
       } 
       { 
        System.out.print("Press 1 if you would like sugar for $.50 or 2 for no: "); 
        userInput = scan.nextInt(); 
        if (userInput == 1) 
        { 
        sugarQty = sugarQty + 1; 
        } 
       }//end case 1 
       break; 

     // espresso is ordered ask for double shot 
      case '2': 
       { 
        espQty = espQty +1; 
        System.out.println("Press 1 for a double shot for $1.25 or 2 for no: "); 
        userInput = scan.nextInt(); 
        if(userInput == 1) 
        { 
        dblshotQty = dblshotQty +1; 
        } 

       }//end case 2 
       break; 

     //tea is ordered 
      case '3': 
       { 
        teaQty = teaQty + 1; 
        System.out.println("You have selected tea! Great Choice."); 
       }//end case 3 
     }//end switch 


     // create output display for total bill adding all totals 


    public void display() 
    { 
     double totalCoff = coffQty * coff + cream * creamQty + sugar * sugarQty; 
     double totalEsp = espQty * esp + dblshot * dblshotQty; 
     double totalTea = teaQty * tea; 

     System.out.println("Order: \n "+coffQty + " Coffee" 
      + "\n "+creamQty +" Cream" 
      + "\n "+sugarQty + " Sugar" 
      + "\nTotal Coffee: "+ totalCoff); 
     System.out.println(" "+teaQty + " Tea" 
      + "\nTotal Tea: "+ totalTea); 
     System.out.println(" "+espQty + " Espresso" 
      + "\n "+dblshotQty +" Double shot" 
      + "\nTotal Espresso: "+ totalEsp); 
     double totalBill = totalCoff+totalEsp+totalTea; 
     System.out.println("\nTotal drink order: "+totalBill); 
    } 



     break; 
    } // end while 
    } 



} // end of class 
+2

您在上面的行上有錯誤。在這個函數上面顯示代碼。 – Carcigenicate

+0

你不必解釋背景。請儘量簡明地(但清楚地)說明問題,以便我們不必對其進行篩選 –

+1

同意@Carcigenicate。通常情況下,「非法開始行」意味着前一行有一些內容會干擾該行的解析。例如,缺少分號或括號,或者只是一個不完整的語句。 – ostrichofevil

回答

0

我看到的幾個主要問題:

  1. 您的main內部定義display,while循環的內部。你不能在方法內部定義方法,更不用說在循環內部了。在main之外移動display,但仍在課堂內。

  2. 你有錯位break漂浮在display下。擺脫這一點,因爲這也將是一個錯誤。

  3. 正如@MininimalLogic指出的那樣,您依靠displaymain的數據。您需要將main的數據作爲參數傳遞給display

+0

我確實試圖把顯示器放在第一個花括號之外,並且在課堂內部,我仍然得到相同的錯誤。我應該將我的聲明移動到類區域(公共主靜態void中的第一個大括號) – Elements

+0

@Elements由於縮進是關閉的,因此很難準確告訴代碼是什麼,但顯示應該在'main'下面。 ,但是在課堂上。 – Carcigenicate

+0

@Elements也需要刪除'display'下的隨機'break'。這也會導致錯誤。 – Carcigenicate

0

您的代碼結構正在讓您感到困難。正如@Carcigenicate所說,(好名字btw),你已經在主要方法中聲明瞭一個方法。一旦你改變了,你會注意到現在display方法顯示編譯器錯誤。這是因爲display中引用的變量不再可見。你有幾個選擇:

  • 重新思考類的設計即應該存在什麼方法以及它們將如何被調用。
  • 將display方法放在main方法的外部,然後使變量成員變量對您需要的方法可見。首先閱讀有關使用成員變量的影響。
  • 完全移除顯示方法並將邏輯移至主方法。
+0

不知道該評論是否是諷刺。之前我曾有人冒犯過它,所以我永遠不知道。 – Carcigenicate

+0

哈哈沒有它的真棒..有時我覺得盯着我的顯示器是致癌的,所以我w /亞.. – MiiinimalLogic

+0

好吧好大聲笑。我需要爲我的用戶名編寫一個單詞,它聽起來很酷。儘管在那裏有詞根,但我從來不認爲它與癌症有關。 – Carcigenicate