【问题标题】:replacing letter(s) in words for hangman error为刽子手错误替换单词中的字母
【发布时间】:2013-06-25 02:10:04
【问题描述】:
 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());
        }
    }
}

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

谢谢

【问题讨论】:

  • 请提供一些示例输入和输出。
  • 如果他们猜出他们已经输入的字母会发生什么,您可能还想考虑一下?目前下面的所有解决方案都不会做任何事情,因为字母已经被替换,但你可能希望它是一个错误的猜测或显示他们已经猜到了

标签: java string object char


【解决方案1】:

由于您的“伪装”仅包含 ?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++;
    }
}

【讨论】:

    【解决方案2】:

    你的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++;
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

      返回一个新字符串,该字符串由替换所有出现的 此字符串中的 oldChar 与 newChar。如果字符 oldChar 不 出现在此 String 对象表示的字符序列中, 然后返回对该 String 对象的引用。否则,一个新的 创建表示字符序列的字符串对象 与此 String 对象表示的字符序列相同, 除了 oldChar 的每次出现都被一个出现替换 新字符。

      【讨论】:

        【解决方案4】:

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

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

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

        要么声明伪装为 StringBuilder 使用 disguise.setCharAt(i, input); 要么做 disguise = disguise.substring(0,i) + input + disguise.substring(i+1, disguise.length());

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

        希望对你有帮助。

        干杯

        【讨论】:

          【解决方案5】:

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

          为此,您可以使用 char[] 代替字符串,并轻松地按索引替换字符。要从 String 中获取 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++;
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-31
            • 2015-11-20
            • 1970-01-01
            相关资源
            最近更新 更多