2016-04-27 131 views
-2

我正在研究一个简单的程序,同样的问题会不断出现。它强调在input.close();中关闭这个词,我会通过我的程序进行搜索,尝试找到任何错误,但无济于事。这里是我的代码:令牌“关闭”的语法错误,此令牌之后的标识符预期

import java.util.Scanner; 

    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 

     String runProgram = ""; 
     String itemDesc = ""; 
     char cookieType = ' '; 
     char shirtType = ' '; 
     char itemChoice = ' '; 
     char sizeChoice = ' '; 
     double itemPrice = 0.0; 
     double totalSales = 0.0; 
     int totalSigned = 0; 
     int totalLemonade = 0; 
     int totalChocChip = 0; 
     int totalOatmeal = 0; 
     int totalRegShirt = 0; 
     int totalTrans = 0; 

     //Prime read of item selection 
     itemChoice = getMenuSelection(input); 
     while (itemChoice != 'Q') 
     { 
      if (itemChoice == 'L') 
      { 
       sizeChoice = getCupSize(input); 
      } 
      else if (itemChoice == 'C') 
      { 
       cookieType = getCookieType(input); 
      } 
      else 
      { 
       shirtType = getShirtType(input); 
      } 

      //displayResults(i 
       System.out.print("Great, your order is as follows: "); 
       System.out.print("Item Purchased " + itemDesc); 
       System.out.print("Item Price $" + itemPrice); 
       System.out.print(" "); 
       totalTrans = totalTrans + 1; 
       totalSales = totalSales + itemPrice; 
      itemChoice = getMenuSelection(input); 
     } //END WHILE 

     System.out.printf("\n%-30s%30s\n", "Total Number of Transactions", totalTrans); 
     System.out.printf("\n%-30s%30s\n", "ITEM DESCRIPTION", "QUANTITY SOLD"); 
     System.out.printf("%-30s%23s%s\n", "Lemonade", totalLemonade, " ounces"); 
     System.out.printf("%-30s%30s\n", "Chocolate Chip Cookies", totalChocChip); 
     System.out.printf("%-30s%30s\n", "Oatmeal Cookies", totalOatmeal); 
     System.out.printf("%-30s%30s\n", "Plain Shirts", totalRegShirt); 
     System.out.printf("%-30s%30s\n", "Autographed Shirts", totalSigned); 
     System.out.printf("\n%-30s%25s%.2f\n\n", "TOTAL SALES", "$", totalSales); 

     //displayResults(totalSales, totalSigned, totalLemonade, totalChocChip, totalOatmeal, totalRegShirt, totalTrans); 
     System.out.println("Goodbye and thank you for visiting Lemonade Express."); 

    }//END MAIN 

    /** 
    *  Input the itemChoice. Accept only Q, L, C, or T 
    * 
    *@return One of four characters, shown above 
    */ 
    static char getMenuSelection(Scanner consoleInput) 
    { 
     Scanner input = new Scanner(System.in); 
     char itemChoice; 

     System.out.print("Please enter purchase selection: "); 
     System.out.print("  (L) Lemonade"); 
     System.out.print("  (C) Cookie"); 
     System.out.print("  (T) T-Shirt"); 
     System.out.print("  (Q) Quit"); 
     itemChoice = consoleInput.nextLine().charAt(0); 
     itemChoice = Character.toUpperCase(itemChoice); 

      //tests for valid entry 
      while (itemChoice != 'L' && itemChoice != 'C' && itemChoice != 'T' && itemChoice != 'Q') 
      { 
       System.out.print("Entered wrong item choice! "); 
       System.out.print("Enter item choice: "); 
       itemChoice = consoleInput.nextLine().charAt(0); 
       itemChoice = Character.toUpperCase(itemChoice); 
      } 
     return itemChoice; 
    } //END getMenuSelection 

    /** 
    *  Input and return Cup Size 
    * @param consoleInput object to recieve read from console 
    *  @return for one lemonade sale 
    */ 
    static char getCupSize(Scanner consoleInput) 
    { 
     Scanner input = new Scanner(System.in); 
     char sizeChoice; 
     System.out.print("What size lemonade would you prefer:"); 
     System.out.print("  (S) 12 ounce"); 
     System.out.print("  (L) 16 ounce"); 
     sizeChoice = consoleInput.nextLine().charAt(0); 
     sizeChoice = Character.toUpperCase(sizeChoice); 

      //tests for valid entry 
      while (sizeChoice != 'S' && sizeChoice != 'L') 
      { 
       System.out.print("Entered wrong item choice! "); 
       System.out.print("Enter size choice: "); 
       sizeChoice = input.nextLine().charAt(0); 
       sizeChoice = Character.toUpperCase(sizeChoice); 
      } 
     String itemDesc; 
     double itemPrice; 
     int totalLemonade; 
     if (sizeChoice == 'S') 
      { 
       itemDesc = "12 ounce lemonade"; 
       itemPrice = 1.50; 
       totalLemonade = totalLemonade + 12; 
      } 
     else 
      { 
       itemDesc = "16 ounce lemonade"; 
       itemPrice = 2.00; 
       totalLemonade = totalLemonade + 16; 
      } 
      return sizeChoice; 
    } 

    /** 
    *  Input and return Cookie Type 
    * @param getCookieType to find price for cookies 
    *  @return for one cookie sale 
    */ 
    static char getCookieType(Scanner consoleInput) 
    { 
     Scanner input = new Scanner(System.in); 
     char cookieType; 
     System.out.print("Which kind of cookie would you prefer?"); 
     System.out.print(" (C) chocolate chip"); 
     System.out.print("  (O) oatmeal"); 
     cookieType = consoleInput.nextLine().charAt(0); 
     cookieType = Character.toUpperCase(cookieType); 
     double itemPrice = .75; 
     while (cookieType != 'S' && cookieType != 'L') 
     { 
      System.out.print("Entered wrong item choice! "); 
      System.out.print("Enter cookie choice: "); 
      cookieType = input.nextLine().charAt(0); 
      cookieType = Character.toUpperCase(cookieType); 
     } 
     String itemDesc; 
     if (cookieType == 'S') 
      { 
       itemDesc = "Chocolate Chip Cookie"; 
       int totalChocChip = totalChocChip + 1; 
      } 
     else 
      { 
       itemDesc = "Oatmeal Cookie"; 
       int totalOatmeal = totalOatmeal + 1; 
      } 
      return cookieType; 
    } 

     /** 
     *  Input and return Shirt Type 
     * @param getShirtType to find price for shirts 
     *  @return for one shirt sale 
     */ 
    static char getShirtType(Scanner consoleInput) 
     { 
      Scanner input = new Scanner(System.in); 
      char shirtType; 
      System.out.print("Which kind of shirt would you prefer?"); 
      System.out.print("  (A) autographed t-shirt"); 
      System.out.print("  (N) normal t-shirt"); 
      shirtType = input.nextLine().charAt(0); 
      shirtType = Character.toUpperCase(shirtType); 
      while (shirtType != 'A' && shirtType != 'N') 
      { 
       System.out.printf("\n%s", "Entered wrong item choice! "); 
       System.out.print("Enter shirt choice: "); 
       shirtType = input.nextLine().charAt(0); 
       shirtType = Character.toUpperCase(shirtType); 
      } 
      double itemPrice; 
      String itemDesc; 
      if (shirtType == 'Y') 
      { 
       itemPrice = 15.00; 
       itemDesc = "Autographed T-shirt"; 
       int totalSigned = totalSigned + 1; 
      } 
      else 
      { 
       itemPrice = 8.00; 
       itemDesc = "Regular T-Shirt"; 
       int totalRegShirt = totalRegShirt + 1; 
      } 
      return shirtType; 
     } 
    input.close(); 
} 

感谢您的帮助提前!

+2

缩进正确会告诉你该行不在方法内。 –

+0

你的类声明在哪里? – shmosel

+1

文件末尾的input.close()超出了方法。 – alpert

回答

0

input.close(); 

在类的到底是不是在一个方法,这是Java不允许。

您需要将其移至main方法。它应该将while循环结束后可能会去,你已经在这一点上与输入完毕:

} //END WHILE 

    input.close(); 

    System.out.printf("\n%-30s%30s\n", "Total Number of Transactions", totalTrans); 

一旦你做到了这一点,你会发现还有其他的编译错误,当你有固定的Eclipse只能检测第一个错误。

相关问题