小程序没有运行:Stanford Engineering Everywhere Hangman

时间:2011-06-16 06:08:15

标签: java applet

我写过一个java applet,它似乎在运行时打开sun.applet.AppletViewer。我正在使用acm库进行斯坦福工程无处不在的Hangman任务。我遇到的问题似乎与代码无关,除非我没有做过一些我应该做的实现。基本上Applet Viewer只是说“启动applet ...”并且程序没有运行。我之前没有遇到过这个问题,所以我想我会问这里以防万一有人遇到过这样的问题。我想这可能是抛出的默认异常情况,但我没有收到错误消息,所以我对此表示怀疑。

我认为代码应该正常工作,但我会发布它以防万一我已经遗漏了一些明显的东西。我还没有完成作业,但我相信如果我没有这个问题它会起作用。

Hangman.java

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

import java.awt.*;
import java.io.*;


public class Hangman extends ConsoleProgram {

//private static final int NUM_GUESSES=8;

/*new instance of HangmanLexicon.java */

private HangmanLexicon hangLex; 

private HangmanCanvas canvas;

/*new random generator to get indexes of words in hangLex*/
//private RandomGenerator rgen = RandomGenerator.getInstance();

/*Stores word  that use is trying to guess*/
private String hidden="";

/*Displayed version of hidden*/
private String displayHidden = "";

/*prompt for player to enter their guess, which gets stored
 * as a string*/
private String guess = readLine("Your guess: ");

/*counts down until user has no more guesses*/
private int guessCounter = 8;

private String repeat = readLine("Play again? (y or n): ");

public void init() {
    canvas = new HangmanCanvas();
    add(canvas);
}

/*Runs through program, acts as main*/
public void run() {
    /* You fill this in */


    println("Welcome to the Hangman program!");
    while(true) {
        setUpWord();
        displayWord();
        guessesLeft();
        guess = readLine("Your guess: ");
        println(guess);
        checkGuess();
        if(displayHidden.equals(hidden)) {
            println("You guessed the word: " + hidden);
            println("You win.");
            println(repeat);
            if(!repeat.equals("n")) {
                guessCounter=8;
                canvas.reset();
            }else {
                break;
            }
        } else if (guessCounter==0) {
            println("The word was: " + hidden); 
            println("You lose.");
            println(repeat);
            if(!repeat.equals("n")) {
                guessCounter=8;
                canvas.reset();
            }else {
                break;
            }
        }
    }

}



/*sets hidden to a random word in hangLex*/
private void setUpWord() {
    hangLex = new HangmanLexicon();
    hidden=hidden.concat(hangLex.getWord(8));
}

/*Prints the updated version of the known word after a guess is entered*/
private void displayWord() {
    if (displayHidden.equals("")) {
        for (int i= 0; i<hidden.length(); i++) {
            displayHidden = displayHidden.concat("-");
        }
        println("The word now looks like this: " + displayHidden);
    } else if(guessCounter==0) {

    } else {
        println("The word now looks like this: " + displayHidden);

    }
}


/*prints updates on the number of guesses the user has left to console*/
private void guessesLeft() {
    if (guessCounter > 1) {
        println("You have " + guessCounter + " guesses left.");
    } else if (guessCounter == 1) {
        println("You have only one guess left.");
    } else if (guessCounter == 0) {
        println("You're completely hung.");
    }
}


private void checkGuess() {
    int letterCounter =0;
    while (guess.length()>1) {
        println("Guesses must be single characters.");
        println(guess);
    }
    char checkLetter = guess.charAt(0);
    guessCounter--;

    //
    if(Character.isLowerCase(checkLetter)) {
        Character.toUpperCase(checkLetter);
    }

    //
    for(int i=0; i<hidden.length(); i++) {
        if(hidden.charAt(i) == checkLetter) {
            displayHidden = displayHidden.substring(0, i) + guess + 
                                        displayHidden.substring(i+1);
        } else {
            letterCounter++;
        }
    }

    //
    if (letterCounter==hidden.length()) {
        println("There are no " + checkLetter + "'s in the word.");
    } else if (letterCounter!=hidden.length()) {
        println("That guess is correct.");
    }

}
}

HangmanLexicon.java

import acm.util.*;

public class HangmanLexicon {


/** Returns the number of words in the lexicon. */
public int getWordCount() {
    return 10;
}

/** Returns the word at the specified index. */
public String getWord(int index) {
    switch (index) {
        case 0: return "BUOY";
        case 1: return "COMPUTER";
        case 2: return "CONNOISSEUR";
        case 3: return "DEHYDRATE";
        case 4: return "FUZZY";
        case 5: return "HUBBUB";
        case 6: return "KEYHOLE";
        case 7: return "QUAGMIRE";
        case 8: return "SLITHER";
        case 9: return "ZIRCON";
        default: throw new ErrorException("getWord: Illegal index");
    }
};
 }

我还没有完成其他文件,但是在我解决了applet之前,我不应该关注这个问题。

1 个答案:

答案 0 :(得分:0)

没关系。我解决了这个问题。我应该在run方法中这样做。

guess = readLine("Your guess: ");

这对我来说是一个非常愚蠢的错误。大声笑。我以为我已经尝试过了。