显示账单总额,Java 公共无效显示() 错误



我在执行总账单显示时遇到一些问题。我问我的教授谁给了我这一小块代码,我修改了它以满足我的需求,但由于某种原因它没有执行并且有错误:

行 --> 公共 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

你的代码结构让你很难。 正如@Carcigenicate(顺便说一下(提到的,你在main方法中声明了一个方法。 更改后,您现在会注意到现在 display 方法显示编译器错误。这是因为display中引用的变量不再可见。您有以下几种选择:

  • 重新思考类的设计,即应该存在哪些方法以及如何调用它们。
  • 将显示方法置于 main 方法之外,然后使变量成员变量对所需方法可见。 首先阅读有关使用成员变量的影响。
  • 完全删除显示方法,并将逻辑移动到主方法。

我看到了几个主要问题:

  1. 您在 main 内部定义了 display ,在 while 循环中。不能在方法内部定义方法,更不用说在循环内部了。将display移出main,但仍在类内。

  2. 你有一个放错地方的break漂浮在display下面.摆脱它,因为那也将是一个错误。

  3. 正如@MiiinimalLogic指出的那样,您依赖于来自display内部main的数据。您需要将数据作为参数从main传递到display

最新更新