为什么我的代码没有按预期工作?

时间:2014-01-11 04:44:15

标签: java structure

所以我写了一个小的Hangman游戏,但我没有CLUE为什么它不起作用。通常我会问一个问题来解决问题,但我根本不知道发生了什么以及为什么我遇到这个问题。没有错误。

以下是我的所有代码:

 public static void main(String[] args) {
    Scanner sc = new Scanner (System.in);
    String mysteryGuess = "hello";
    String userGuess = "";
    int wrongGuesses;


    System.out.println("Hello and welcome to Hang Man!");
    loop:
    for(;;){
        String[] wordAsArray = convertToStringArray(mysteryGuess);
        boolean[] guessed = new boolean[mysteryGuess.length()];
        for (int i = 0; i<wordAsArray.length;i++)
            if(wordAsArray[i] == userGuess)
                guessed[i]=true;
        System.out.println("Word so far:" + visibleWord(wordAsArray,guessed));
        System.out.println("What is your guess?");
        userGuess = sc.next();
        if (guess(userGuess,wordAsArray,guessed) == true)
            System.out.println("Correct");
        else
            System.out.println("Incorrect");
        if (didWin(guessed)==true)
            break loop;
    }
}


//This method creates an array version of the parameter word
//For example, if word contained the data "hello", then this method
//would return {"h", "e", "l", "l", "o"}
//Parameters:   word - a single word
//Returns:      an array containing each letter in word
public static String[] convertToStringArray(String word) {
    String [] pWord = new String [word.length()];
    for (int i = 0; i<pWord.length; i++){
        pWord[i] = word.substring(i,i+1);
    }
    return pWord;

}


//This method determines whether the player has won the game of HangMan
//Parameters:   guessed - array of boolean values
//Returns:      true - if every value in guessed is true
//              false - if at least one value in guessed is false
public static boolean didWin(boolean[] guessed) {
    boolean bGuess = true;
    loop:
    for (int i = 0; i<guessed.length;i++){
        if(guessed[i]==false){
            bGuess = false;
            break loop;
        }


    }
        return bGuess;
}


//This method determines what portion of the hidden word is visible
//For example, if the parameters are as follows:
//     wordAsArray: {"h", "e", "l", "l", "o"}
//     guessed: {true, false, false, false, true}
//Then the method should return "h???o"
//Parameters:   wordAsArray - the individual letters to be guessed
//              guessed - array of boolean values; a true value means the corresponding letter has been guessed
//Returns:      A string representing how much of the word has been guessed (unguessed letters are represented by ?'s)
public static String visibleWord(String[] wordAsArray, boolean[] guessed) {
    String visibleWord="";
    for(int i = 0; i<wordAsArray.length;i++){
        if (guessed[i] == true)
            wordAsArray[i]=wordAsArray[i];
        if (guessed[i] == false)
            wordAsArray[i]="?";
    }
    for(int i = 0; i<wordAsArray.length;i++){
        visibleWord=visibleWord+wordAsArray[i];
    }
    return visibleWord;
}


//This method checks to see if a player has made a successful guess in the game of Hang Man
//For example, if the parameters are as follows:
//     letter: "e"
//     wordAsArray: {"h", "e", "l", "l", "o"}
//     guessed: {true, false, false, false, true}
//Then the guessed array would be changed to:
//      guessed: {true, true, false, false, true}
//And the method would return false
//Parameters:   letter - the letter that the user has just guessed
//              wordAsArray - an array of individual letters that are to be guessed
//              guessed - array of boolean values; a true value means the corresponding letter has been guessed
//Returns:  true - if letter matches an unguessed letter in wordAsArray
//          false - otherwise
public static boolean guess(String letter, String[] wordAsArray, boolean[] guessed) {
    boolean appearsAtLeastOnce=false;
    for(int i = 0; i<wordAsArray.length;i++)
        if(letter.equalsIgnoreCase(wordAsArray[i])){
            guessed[i] = true;
            appearsAtLeastOnce=true;
        }
    return appearsAtLeastOnce;



}

}

因此,当你输入你的猜测,即使它是正确的,它会说它不正确,“到目前为止”这个词不会表明你弄错了。有人可以向我解释这里有什么问题吗?我确信这是一件我想念的简单事情,但我已经在这几个小时了。谢谢。

另外,我只是用“hello”进行测试。我希望输入各种字典单词列表。

3 个答案:

答案 0 :(得分:3)

更改

if(wordAsArray[i] == userGuess)

if(wordAsArray[i].equals(userGuess)) //Replace .equals with .equalsIgnoreCase for case insensitive compare.

您正在比较字符串,而不是对象。因此==无效。

希望这有帮助。

答案 1 :(得分:1)

您的行if(wordAsArray[i] == userGuess)正在比较两个对象,看它们是否相等。在这种情况下,有两个String对象。此比较检查对象的引用是否相等(内存位置)。尽管字符串具有相同的值,但它们是两个不同的对象,因此具有两个不同的存储位置,这将使它们不相等。

您要做的是检查两个对象是否相同。这可以通过使用String.equals()方法来完成: if(wordAsArray[i].equals(userGuess))

通常,在处理原始数据类型(int,float,boolean,char ...)时,==比较工作正常。但是,在处理对象时,您应该使用对象的equals()方法。

有关原始类型的更多信息:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

答案 2 :(得分:0)

我没有深入审核您的代码,但是您的代码存在一些问题(可能还有一些问题):

  1. 最重要的是你为inside false`定义了猜测loop, so every time the array is initialized to all
  2. 您正在使用==用于字符串,这将无法使用,您应使用String.equals()方法代替,为什么不使用char s?
  3. java.lang.StringtoCharArray(),返回代表每个字符的char[],因为char是原始的,您可以在其上使用==运算符。