2016-04-27 31 views
-2

シンプルなプログラムで作業していて、同じ問題が引き続き発生します。それはinput.close();の中でcloseという単語に下線を引いています。私は自分のプログラムを検索してエラーを見つけようとしますが、役に立たないのです。私のコードは以下の通りです:トークン "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が唯一検出することができ、他のコンパイルエラーがあるでしょう最初のエラー。

関連する問題