错误:无法找到或加载主类hangman.java

7d7tgy0s  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(204)

大家好,当我试图运行这个文件时,出现了一个错误“error:couldnotfind or load main class hangman.java”。它编译得很好。您能告诉我缺少什么吗。这是一个具有以下属性的刽子手游戏
-密语
-隐藏的单词,其中秘密单词中的每个未知字母都被问号(?)取代。
例如,如果秘密单词是“abracadabra”,字母a、b和e已经猜到了,那么伪装的单词将是ab?a?a?ab?a。-猜测的次数-错误的猜测次数
它将有以下方法:
-makeguess(c)猜测字符c在单词中
-getdword返回一个字符串,其中包含在正确位置正确猜测的字母,以及替换为“”的未知字母
-getsecretword返回秘密单词
-getguesscount返回所做猜测的次数
-如果发现了隐藏单词,isfound返回true

import java.util.*;

public class Hangman {
   private String secretWord;
   private String disguisedWord ="";
   private String guessedLetters = "";
   private int numberOfGuesses = 0;
   private int numberOfIncorrectGuesses = 0;

   Hangman(String secretWord){
     this.secretWord = secretWord;
     for (int i = 0; i < secretWord.length(); i++){
       this.disguisedWord += "?";
     }
   }

   public String getSecretWord() {
        return secretWord;
   }

   public String getGuesses() {
        return guessedLetters;
   }

   public String getDisguisedWord() {
        return disguisedWord;
   }

   public int getGuessCount() {
        return numberOfGuesses;
   }

   public int getIncorrectGuessCount() {
        return numberOfIncorrectGuesses;
   }

   public boolean isFound() {
        if (secretWord.equalsIgnoreCase(disguisedWord)){
            return true;
        } else{
            return false;
        }
   }

   public boolean makeGuess(char guess) {

        for (int i = 0; i < guessedLetters.length(); i++){
            if (guess == guessedLetters.charAt(i)) {
                System.out.println("You have already guessed that letter.");
                return false;
             }
        }

        guessedLetters+= guess;
        String tempString = "";
        numberOfGuesses++;
        for (int i = 0; i < secretWord.length(); i++) {
                if (guess == secretWord.charAt(i)){
                    tempString += guess;
                } else {
                    tempString += disguisedWord.charAt(i);
                }
        }
        if (!tempString.equalsIgnoreCase(disguisedWord)) {
                disguisedWord = tempString;
                return true;
        } else {
                System.out.println("Incorrect Guess!");
                numberOfIncorrectGuesses++;
                return false;
        }
    }

    public static void main(String[] args) {
        java.util.Scanner keyboard = new java.util.Scanner(System.in);
        Hangman hangman = new Hangman("pirate"); // enter secret word as a string

        while (hangman.isFound()== false){
            System.out.println("The word is: " + hangman.getDisguisedWord());
            System.out.println("Guess a letter or type 'guesses' to see your guesses.");
            String input = keyboard.nextLine();
                if (input.equalsIgnoreCase("guesses"))
                {
                    System.out.println(hangman.getGuesses());
                }
                else
                {
                    hangman.makeGuess(input.charAt(0));
                }
            }

            System.out.println("The secret word was: " + hangman.getSecretWord());
            System.out.println("It took you " + hangman.getGuessCount() + " guesses to get the correct answer.");
            System.out.println("You had " + hangman.getIncorrectGuessCount()+ " incorrect guesses.");
    }
}
siv3szwd

siv3szwd1#

运行程序时应使用: java -cp . Hangman 而不是 java Hangman.java 重要的是要明白
您可以使用 javac <classname>.java 您可以使用 java <classname> (请注意,运行程序时不需要.java)

相关问题