简体   繁体   English

令牌“关闭”上的语法错误,此令牌后应有标识符

[英]Syntax error on token "close", Identifier expected after this token

I was working on a simple program and the same problem would keep coming up.我正在开发一个简单的程序,同样的问题会不断出现。 It underlines the word close in input.close();它在input.close();强调关闭这个词input.close(); and I'd search through my program to try and find any errors but to no avail.我会搜索我的程序以尝试查找任何错误,但无济于事。 Here's my code:这是我的代码:

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();
}

Thanks for the help in advance!我在这里先向您的帮助表示感谢!

Your您的

input.close();

at the end of the class is not in a method, this is not allowed in Java.类的末尾不在方法中,这在 Java 中是不允许的。

You need to move this to your main method.您需要将其移至您的main方法。 It should probably go after the end of your while loop as you have finished with input at that point:当您完成输入时,它可能应该在您的while循环结束之后进行:

   }   //END WHILE

   input.close();

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

Once you have done this you will find there are other compile errors which Eclipse can only detect when you have fixed the first error.完成此操作后,您会发现还有其他编译错误,Eclipse 只能在您修复第一个错误时检测到这些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM