显示账单总计,Java public void display()错误

时间:2017-04-14 13:17:56

标签: java void public

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

  

非法开始表达行 - > 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 个答案:

答案 0 :(得分:0)

我看到一些主要问题:

  1. 您在display内部定义了main,在while循环中。您无法在方法内部定义方法,更不用说在循环内部。将display移到main之外,但仍在课堂内。

  2. break下的display错位main。摆脱它,因为这也将是一个错误。

  3. 正如@MiiinimalLogic指出的那样,您依赖于displaymain的数据。您需要将数据从display传递给public class Person { public string Name { get; set; } public List<Address> Addresses { get; set; } } public class Address { public string City { get; set; } } 作为参数。

答案 1 :(得分:0)

您的代码结构让您感到困难。正如@Carcigenicate所提到的(好名字btw),你已经在main方法中声明了一个方法。一旦你改变了,你现在会注意到display方法现在显示编译器错误。这是因为display中引用的变量不再可见。您有几个选择:

  • 重新思考班级的设计,即应该存在哪些方法以及如何调用它们。
  • 在main方法之外取显示方法,然后使变量成员变量对您需要的方法可见。首先阅读使用成员变量的影响。
  • 完全删除显示方法并将逻辑移至main方法。
相关问题