6

编写一个使用 Book、Tape 和 CD 类来创建对象的 BookStoreApplication。虽然未完成,但应用程序类应该创建新的 BookStoreItems,它们是 Book、Tape 和 CD。它们继承自 BookStoreItems 类。在这个应用程序类中,我不断收到错误:

error: non-static method printMenu() cannot be referenced from a static context
error: non-static method getUserChoice() cannot be referenced from a static context
error: non-static variable input cannot be referenced from a static context

我已将其更改为静态而不是静态,但我继续收到此错误...

import java.util.Scanner;

public class BookStoreApp2 {

    //constants for options
    static final int ADD_BOOK = 0;
    static final int ADD_TAPE = 1;
    static final int ADD_CD = 2;
    static final int QUIT = -1;

    Scanner input = new Scanner (System.in);

    public static void main(String[] args) {


        BookStoreItem[] item;//declaring array

        item = new BookStoreItem[10];//initializing array

        int itemType = -1;

        printMenu();
        getUserChoice();

        for (int i = 0; i < item.length; i++){
            System.out.print("\n" + i + "\tEnter 0 for Book, 1 for Tape, 2 for CD: ");
            itemType = input.nextInt();

            switch (itemType) {
                case 0:
                    item[i] = new Book();
                    break;
                case 1:
                    item[i] = new Tape();
                    break;
                case 2:
                    item[i] = new CD();
                    break;
                default: 
                    System.out.println("\nInvalid choice.");
            }//end of switch statement

        }//end of for loop

        for (int i = 0; i < item.length; i++) {
            System.out.println("\nAnimal #" + i + ": ");

            System.out.println("\n\tTitle: " + item[i].getTitle()); //polymorphic because they can operate on separate objects
            System.out.println("\n\tAuthor: " + item[i].getAuthor());
        }//end of for


    }//end of main method


//PRINT MENU----------------------------------------------------------  
    public void printMenu(){
        System.out.println("\nPress:");
        System.out.println("\t" + ADD_BOOK + "\tTo add a book to the book store.\n");
        System.out.println("\t" + ADD_TAPE + "\tTo add a tape to the book store.\n");
        System.out.println("\t" + ADD_CD + "\tTo add a CD to the book store.\n");
        System.out.println("\t" + QUIT + "\tTo exit\n");
    }
//---------------------------------------------------------------------
//GET USER CHOICE------------------------------------------------------ 
     public int getUserChoice() {
        int choice;     
        System.out.print("Please enter your choice: ");
        choice = input.nextInt();

        return choice;
    }//end of getUserChoice
//----------------------------------------------------------------------

}//end class
4

4 回答 4

18

您需要同时创建您的方法 -printMenu()getUserChoice() static,因为您直接从您的static main方法调用它们,而不创建类的实例,这些方法是在其中定义的。并且您不能在non-static不引用它们的类实例的情况下调用方法中定义。

或者,您可以将方法调用部分更改为:

BookStoreApp2 bookStoreApp = new BookStoreApp2();
bookStoreApp.printMenu();
bookStoreApp.getUserChoice();
于 2013-02-13T20:19:55.967 回答
5

仅仅为了使您的程序工作,获取您的 main() 方法的内容并将它们放入构造函数中:

public BookStoreApp2()
{
   // Put contents of main method here
}

然后,在您的 main() 方法中。做这个:

public void main( String[] args )
{
  new BookStoreApp2();
}
于 2013-02-13T20:22:19.873 回答
2

你可以

1) 声明printMenu(),getUserchoice()并输入为static

或者

2)如果您想更好地设计它,请将逻辑从您的逻辑移到main单独的实例方法中。然后从main创建您的类的新实例并调用您的实例方法

于 2013-02-13T20:21:48.587 回答
1

您应该Scanner input = new Scanner (System.in);放入 main 方法而不是在外部创建输入对象。

于 2013-02-13T20:28:26.533 回答