简体   繁体   中英

Could not find or load main class in java animal guessing game

I keep getting this error message

Error: Could not find or load main class Animals.Animals Java Result: 1

I don't think I did anything wrong to my program. I can't even find where I did something wrong. Here's my program:

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?"));
}

}

The Animals class MUST be in the Animals directory...

\src
    \Animals
        Animals.java

It must have the package decleation of package Animals

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

You can compile it from within the Animals directory, but it would probably be safer to compile it in the parent directory...

javac -cp Animals Animals\*.java

You then need to use the fully qualified class name to run it (from the parent directly of Animals...)

java Animals.Animals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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