令牌上的语法错误"关闭",此令牌后预期的标识符

时间:2016-04-27 02:38:19

标签: java eclipse

我正在研究一个简单的程序,同样的问题会不断出现。它强调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();
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你的

main
在类的末尾

不在方法中,这在Java中是不允许的。

您需要将其移至while方法。它可能应该在 } //END WHILE input.close(); System.out.printf("\n%-30s%30s\n", "Total Number of Transactions", totalTrans); 循环结束后继续,因为您已完成输入:

Intent i = new Intent(getActivity(), RegisterApp.class);
getActivity().startService(i);

完成此操作后,您会发现还有其他编译错误,Eclipse只有在您修复了第一个错误后才能检测到。