我试图在java eclipse中编译这段代码时遇到错误。这是一个Hangman项目

时间:2011-11-25 05:21:56

标签: java eclipse

这是我的代码。对于帮助我解决这个问题的人,我将非常感激。 非常感谢。我刚开始编写代码。我正在尝试制作一个刽子手项目,我觉得我正在这样做,但它只是不编译。请帮忙

import java.io.*;
import java.util.*;


public class Hangman {

    public static void main(String[] args) throws FileNotFoundException {

        String[] dictionary = loadWords();
        Scanner kb = new Scanner(System.in);
        System.out.println("Welcome to Hangman!");
        System.out.println("How many tries");

        String word = chooseWord(dictionary);
        System.out.println("OK, I've choseen my word. Start guessing");
        System.out.println(word);
        String wordguess = kb.next();
        char guess = wordguess.charAt(0);
        boolean[] bool = new boolean[26];
        processGuess(guess, word, bool);




        // FINISH IMPLEMENTING THIS!
    }

    // Loads a list of words from a file and returns the lsit as a String[].
    // DO NOT ALTER THIS IMPLEMENTATION!!
    public static String[] loadWords() throws FileNotFoundException {
        ArrayList<String> wordList = new ArrayList<String>();
        Scanner wordFile = new Scanner(new File("dictionary.txt"));
        String line;
        while (wordFile.hasNextLine()) {
            line = wordFile.nextLine();
            if(!line.equals(""))   
                wordList.add(line);
        }

        String[] result = new String[1];
        return wordList.toArray(result);
    }
    // Takes an array of strings that represents the valid words as a parameter.
    // Chooses one such word randomly and returns it.
    public static String chooseWord(String[] dict) {

        Random r = new Random();
        int numword = r.nextInt(dict.length); 
        String hangword = dict[numword];  
        return hangword;

    }

    // Checks if a player has won the game
    // Returns true only if all letters in the word have been guessed
    public static boolean hasWon(String word, boolean[] guesses) {

        for(int i = 0; i < word.length(); i++) {
        if (guesses[word.charAt(i)-'a'] == false){
            return false;
            }
        }   
        return true;
    }

    // Prints out the pattern of letters in the secret word based on the word 
    //    and the letters that have been guessed.
    // Prints any letter that has already been guessed and a _ for a letter that 
    //    has not been guessed.
    public static void printPattern(String word, boolean[] guesses) {
        StringBuilder pattern = new StringBuilder();
        for(int i = 0; i < word.length(); i++) { 
            if (guesses[word.charAt(i) - 'a']) { 
                pattern.append(word.charAt(i));
            }
            else {
                pattern.append("_");
            }
            pattern.append(" ");
            }

        System.out.println(pattern.toString());
    }



    // Handles a guess by marking the letter as guessed and returns the number of 
    //    tries to be charged: 0 if the guessed letter is in the word and 1 otherwise. 
    public static int processGuess(char guess, String word, boolean[] guesses) {        
        int holdvalue = 0;
        if (guesses[guess - 'a']) {
            return 0;
        }

        for(int i = 0; i < word.length(); i++) {
            if (word.charAt(i) == guess) {
                holdvalue ++;
            }
        }
        if (holdvalue >= 0){
            return 0;
        }
        else {
            return 1;
        }

    }
}

1 个答案:

答案 0 :(得分:1)

我看到了一些问题

String[] result = new String[1];

由于您已声明大小为1的数组,因此只会从字典中加载一个单词。这可能是

String[] result = new String[wordList.size()];

更多的是你正在从用户那里获取String输入并且你在数组中存储了String,你可以简单地比较它们,可能没有必要逐个字符地比较(如果我没有误解你的目标)

String a = "something";
String b =  get from array.

if( a!=null && a.equals(b))
{ }

您可能想解释确切的问题,这将有助于我们帮助您。