2017-04-14 52 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