无法在java动物猜谜游戏中找到或加载主类

时间:2015-02-13 04:08:38

标签: java compiler-errors

我一直收到此错误消息

  

错误:找不到或加载主类Animals.Animals   Java结果:1

我认为我的计划没有做错任何事。我甚至无法找到我做错的地方。这是我的计划:

package Animals;

import java.util.*;
public class Animals {

  private static final Scanner keyboard = new Scanner(System.in);

public static void introduction() {
    System.out.println("WELCOME TO GUESS THE ANIMAL GAME");
    System.out.println("If I am correct press Y and if I am wrong press N");
    System.out.println("Ready? Let's begin!");
    System.out.println("-------------------------------------------------");
}

public static void letsPlay(AnimalNode<String> latest) {
    while (!latest.correct()) {
        if (query(latest.getAnimal())) {
            latest = latest.getleft();
        } else {
            latest = latest.getright();
        }
    }
    System.out.println("Is it a " + latest.getAnimal());
    if (!query("\nY or N?")) {
        question(latest);
    } else {
        System.out.println("Winner!");
    }
}

public static AnimalNode<String> AnimalTree() {
    AnimalNode<String> root;
    AnimalNode<String> child;
    final String rootQuestion = "dog";
    final String animal = "dog";

    root = new AnimalNode<String>(rootQuestion, null, null);
    return root;
}

public static void question(AnimalNode<String> latest) {
    String setAnimal;
    String correctAnimal;
    String characteristic;

    setAnimal = latest.getAnimal();
    System.out.println("What is the correct answer? ");
    correctAnimal = keyboard.nextLine();

    System.out.println("What's a characteristic of " + correctAnimal
            + " that is different from " + setAnimal);
    characteristic = keyboard.nextLine();

    latest.setAnimal(characteristic);
    System.out.println("A " + correctAnimal + characteristic);
    if (query("Correct?")) {
        latest.setLeft(new AnimalNode<String>(correctAnimal, null, null));
        latest.setRight(new AnimalNode<String>(correctAnimal, null, null));

    }
}

public static boolean query(String ask) {
    String answer;
    System.out.println(ask + "Y or N: ");
    answer = keyboard.nextLine().toLowerCase();
    while (!answer.startsWith("y") && !answer.startsWith("n")) {
        System.out.println("Press the correct letter");
        System.out.println("Let's try again");
    }
    return answer.startsWith("y");
}

public static void main(String[] args) {
    AnimalNode<String> root;
    introduction();
    root = AnimalTree();

    do {
        letsPlay(root);
    } while (query("-------------------------------------------------"
            + "\nPlay again?"));
}

}

1 个答案:

答案 0 :(得分:3)

Animals类必须位于Animals目录...

\src
    \Animals
        Animals.java

必须package取消package Animals

package Animals;
// imports
public class Animals {
    //...

您可以在Animals目录中编译它,但在父目录中编译它可能会更安全...

javac -cp Animals Animals\*.java

然后,您需要使用完全限定的类名来运行它(直接来自动物的父级...)

java Animals.Animals