调用方法“.class expected”

时间:2012-12-06 20:06:16

标签: java class methods

尝试使用以下代码从主类运行start方法时:

public class rungame {
    public void run() {
        Menu.start(string[]);
    }
}

我尝试运行的方法是:

    public class Menu{

    public static void start(String[] args){
        for( ; ; ){
            System.out.println("\nWelcome To The Block Game By Joe Easton\n");
            System.out.println("Enter S To Start");
            System.out.println("Enter H For Help");
            System.out.println("Enter E To Exit");
            System.out.println("\nWhat is your command?");

            //loop looking for input
            for( ; ; ){

                //private
                String answer;
                Keyboard kb = new Keyboard();
                answer = kb.readString();

                if(answer.charAt(0) == 'S' || answer.charAt(0) == 's' || answer.charAt(0) == 'H' || answer.charAt(0) == 'h' || answer.charAt(0) == 'e' || answer.charAt(0) == 'E') {

                    if(answer.charAt(0) == 'S' || answer.charAt(0) == 's'){
                        System.out.println("\nWhat is your name?");
                        String name;
                        name = kb.readString();

                        System.out.println("\nPress y to start");
                        String start;
                        start = kb.readString();

                        if(answer.charAt(0) == 'y' || answer.charAt(0) == 'Y') {

                        }

                        else{break;
                        }
                    }

                    if(answer.charAt(0) == 'H' || answer.charAt(0) == 'h'){

                                    System.out.println("\nIn this game, you have to avoid the B's by moving your character P. \nThe longer you last, the more points you get. \n\nTo move your character:\nUse the G key to go left\nUse the H key to go right\nUse the J key to go up\nUse the K key to go down");
                                    System.out.println("\nWhat is your command?");
                            }

                    if(answer.charAt(0) == 'E' || answer.charAt(0) == 'e'){
                    System.exit(0);
                    }
                }
                else{System.out.println("\nInvalid Command!");
                System.out.println("\nWhat is your command?");
                }
            }
        }
    }
}

当我尝试从另一个类运行它时,我得到一个.class预期错误,菜单类编译并运行正常

2 个答案:

答案 0 :(得分:1)

什么是string[],您应该传递String[]

的有效实例

答案 1 :(得分:0)

如果您正在运行rungame,那么我怀疑它是如何运行的,因为它没有主方法。它确实有一个run方法,但该类既没有实现Runnable接口也没有扩展Thread类。

其他人所说的string[]含糊不清。

此外,如果两个类都在同一个文件中,那么一个文件中只能有一个public类。如果要将public标识符附加到类中,则需要准备单独的文件。

检查以上内容,然后它应该可以正常工作。