用单词替换单词中的letter子手错误 [英] replacing letter(s) in words for hangman error

查看:119
本文介绍了用单词替换单词中的letter子手错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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。更换线。我的代码的目的是通过用户的猜测将伪装的符号替换为秘密的字母。 for循环遍历秘密单词中的所有字母,并在用户输入的字符和秘密单词中的字母之间寻找匹配项。
如果存在匹配项,我希望程序用输入的字符替换在该位置伪装的符号。

I'm having some difficulty with my code, specifically the disguise=disguise.replace line. The objective of my code is to replace the symbol in disguise with the letter in secret via guessing from the user. The for loop goes through all of the letters in the secret word and looks for matches between the user inputted character and the letters in the secret word. If there is a match, I want the program to replace the symbol in disguise at that position with the character from input.

相反,发生的是我的代码将所有伪装的字母替换为用户猜出的字母是否包含在秘密一词中。

Instead what's happening is my code is replacing all the letters in disguise with the letter that the user guesses if it's in the word 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());
        }
    }
}

如果有人可以指出我的替换声明在类编码中是错误的,将不胜感激。

If anyone could point out what is wrong with my replace statement in the class coding, that would be greatly appreciated.

谢谢

推荐答案

由于您的伪装仅包含 replace(char oldchar,char newChar)实际上替换了所有出现的 oldchar ,整个字符串将被替换。

Since your "disguise" contains only ? and replace(char oldchar, char newChar) actually replaces all occurrences of oldchar, your whole string is being replaced.

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

What you really want is to replace a character at a specific position, and for that you could use StringBuilder#setCharAt. See my example below:

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

这篇关于用单词替换单词中的letter子手错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆