0
 public class Hangman {

    private String secret;
    private String disguise;
    private int guessCount;
    private int wrong;

    public Hangman() {
        secret="word";
        disguise="????";
        guessCount=0;
        wrong=0;
    }

    public void makeGuess(char input) {
        String temp;
        temp=disguise;
        for (int i=0; i<secret.length(); i++) {
            if (secret.charAt(i)==input) {
                disguise=disguise.replace(disguise.charAt(i), input);
            }
        }
        if (temp.equals(disguise))
            wrong++;
    }

我的代码有些困难,特别是 disguise=disguise.replace 行。我的代码的目的是通过用户的猜测,用秘密的字母替换伪装的符号。for 循环遍历秘密词中的所有字母,并在用户输入的字符和秘密词中的字母之间查找匹配项。如果匹配,我希望程序用输入中的字符替换该位置的变相符号。

相反,我的代码正在用用户猜测的字母替换所有字母,如果它在单词secret中。

Example:
????
w
wwww (disguise)
word (secret)

what I want:
????
w
w???
word

这是我的演示课:

import java.util.Scanner;
public class HangmanDemo {

    public static void main(String[] args) {
        char input;
        Hangman game = new Hangman();
        Scanner keyboard = new Scanner(System.in);
        System.out.println(game.getDisguisedWord());
        for (int i=0;i<10;i++){
            String line=keyboard.nextLine();
            input = line.charAt(0);

            game.makeGuess(input);
            game.guessCount();
            game.getDisguisedWord();
            game.isFound();
            System.out.println(game.getDisguisedWord());
            System.out.println(game.getSecretWord());
        }
    }
}

如果有人能指出我在类编码中的替换语句有什么问题,那将不胜感激。

谢谢

4

5 回答 5

4

由于您的“伪装”仅包含?并且replace(char oldchar, char newChar)实际上替换了所有出现的oldchar,因此您的整个字符串都被替换了。

您真正想要的是替换特定位置的字符,为此您可以使用StringBuilder#setCharAt. 请参阅下面的示例:

public void makeGuess(char input) {
    StringBuilder temp = new StringBuilder(disguise);
    boolean wrongGuess = true;
    for (int i=0; i<secret.length(); i++) {
        if (secret.charAt(i) == input) {
            temp.setCharAt(i, input);
            wrongGuess = false;
        }
    }

    disguise = temp.toString();
    System.out.println(disguise);
    if (wrongGuess){
        wrong++;
    }
}
于 2013-06-25T02:26:08.537 回答
1

你的disquise词是全部????所以无论你输入什么,代码disguise.charAt(i)总是会是?这样吗?将被替换

你想要的secret.charAt(i)如下

public void makeGuess(char input) {
    String temp;
    temp=disguise;
    char[] disquiseChars = disquise.toCharArray();  //added
    for (int i=0; i<secret.length(); i++) {
        if (secret.charAt(i)==input) {
           //disguise=disguise.replace(secret.charAt(i), input); 
           //Change in above line, but this still wont work, try this now
           disquiseChars[i] = input;
        }
    }
    disquise = disquiseChars;
    if (temp.equals(disguise))
        wrong++;
}

不是最优雅的解决方案,但应该可以解决问题

编辑:下面是检查他们是否已经猜到一个字母的快速方法,但我现在只猜到了一个正确的字母

public void makeGuess(char input) {
    if(diquise.contains(String.valueOf(input))
    {
        System.out.println("You have already guessed " + input);
        wrongGuess++; // if you decide
        return;
    }
    String temp;
    temp=disguise;
    char[] disquiseChars = disquise.toCharArray();
    for (int i=0; i<secret.length(); i++) {
        if (secret.charAt(i)==input) 
        {
           disquiseChars[i] = input;
        }
    }
    disquise = disquiseChars;
    if (temp.equals(disguise))
        wrong++;
}
于 2013-06-25T02:19:36.830 回答
0

字符串 chartAt(i) 返回字符而不是索引。如果替换字符,则所有出现的字符都将被替换。

如果您阅读 Java 文档,那么您必须了解为什么 replace(char, char) 方法不能替换单个字符。以下 qoute 来自 Java Docs。

公共字符串替换(char oldChar,char newChar)

返回一个新字符串,该字符串是用 newChar 替换此字符串中所有出现的 oldChar 所产生的。如果此 String 对象表示的字符序列中没有出现 oldChar 字符,则返回对该 String 对象的引用。否则,将创建一个新的 String 对象,该对象表示与此 String 对象所表示的字符序列相同的字符序列,但每次出现的 oldChar 都替换为出现的 newChar。

于 2013-06-25T02:55:46.933 回答
0

只需将循环中的 Replace 函数替换为 a disguise[i] = input;,随着循环的继续,所有更改都将根据需要进行:)

这里的问题正如 Java Devil 所建议的那样,您要替换所有的“?” 与输入!

编辑:对不起,当我说的时候我在想 .NET disguise[i]

将 Disguise 声明为StringBuilderas usedisguise.setCharAt(i, input);或 Do disguise = disguise.substring(0,i) + input + disguise.substring(i+1, disguise.length());

第二个版本也适用于字符串。您首先从 0 到要更改的位置的索引创建 2 个子字符串,然后添加新字符并将第二个子字符串从 i+1 连接到末尾

希望能帮助到你..

干杯

于 2013-06-25T02:22:14.517 回答
0

查看String.replace(char,char)文档,您会发现它用 newChar 替换了 oldChar 的每个实例。这意味着每个“?” 将替换为“w”。相反,您要做的是仅替换该给定位置的字符。

为此,您可以使用 char[] 代替字符串,并轻松地按索引替换字符。要从字符串中获取 char[],只需调用秘密单词的String.toCharArray()方法。

public class Hangman {

    private String secret;
    private char[] disguise;
    private int guessCount;
    private int wrong;

    public Hangman() {
        secret="word";
        disguise="????".toCharArray(); // <-- Basically this changes
        guessCount=0;
        wrong=0;
    }

    public void makeGuess(char input) {
        boolean found = false;
        for (int i=0; i<secret.length(); i++) {
            if (secret.charAt(i)==input) {
                disguise[i] = input; // <-- Basically this changes
                found = true;
            }
        }
        if (!found)
            wrong++;
    }
于 2013-06-25T02:24:35.433 回答