java - 字母未正确插入到 StringBuilder 中

标签 java stringbuilder

构建 Hangman 游戏时,我在打印用户已经猜对的字母时遇到问题。发生的情况是,用户猜对的第一个字母总是没有问题的。第一个字母之后的正确字母有时会输入,有时则不会,我无法弄清楚一个系统。这可能与延迟有关,这是可能的,因为我的计算机不是很好。

以下是您可能需要的一些变量声明。

StringBuilder orderedLetters = new StringBuilder("_____");
JLabel label = new JLabel(orderedLetters);
String word = "horse";//word is always a randomly generated 5 letter word

这是我认为有问题的方法。

    void printLetterSpaces() {
    //inserts the first letter of the word and deletes the one it moved out of the way
    if      (isLetterSolved(0, 1)) {
        orderedLetters.insert(0, word.substring(0,1));
        orderedLetters.deleteCharAt(1);
    }
    //inserts the second letter of the word and deletes the one it moved out of the way
    else if (isLetterSolved(1, 2)) {
        orderedLetters.insert(1, word.substring(1,2));
        orderedLetters.deleteCharAt(2);
    }
    //inserts the third letter of the word and deletes the one it moved out of the way
    else if (isLetterSolved(2, 3)) {
        orderedLetters.insert(2, word.substring(2,3));
        orderedLetters.deleteCharAt(3);
    }
    //inserts the fourth letter of the word and deletes the one it moved out of the way
    else if (isLetterSolved(3, 4)) {
        orderedLetters.insert(3, word.substring(3,4));
        orderedLetters.deleteCharAt(4);
    }
    //inserts the fifth letter of the word and deletes the one it moved out of the way
    else if (isLetterSolved(4, 5)) {
        orderedLetters.insert(4, word.substring(4,5));
        orderedLetters.deleteCharAt(5);
    }
    lettersLabel.setText(orderedLetters.toString());
    System.out.println(userInput.getText().substring(0, 1));
    userInput.setText("");
}

这是我在上面方法中引用的方法:

boolean isLetterSolved(int x, int y) {
    if(lettersGuessedCorrectly.toString().contains(word.substring(x,y)))
        return true;
    else
        return false;
}

在猜测字母之前: https://gyazo.com/d6d547250e0da4cc0319e1f7e5b2fc14

第一次正确猜出一个字母后(O是我猜到的字母):https://gyazo.com/f2d8535f8a46629ba9fa9d336faa7c2e

第二次正确猜出一个字母后(r是我猜到的字母(它使jlabel变成正确的猜测)):https://gyazo.com/159e7c82c3f07fc5b18df51fbf00bbef

测试字母是否正确的方法是完全正确的,因为 JLabel 不会在不调用 printLetterSpaces 的情况下告诉你你的猜测是正确的

编辑:好吧,我解决了它,我不确定它为什么有效,但这是解决的方法:

void printLetterSpaces() {
    //inserts the first letter of the word and moves the one it replaced out of the way
    if      (isLetterSolved(0, 1)) {
        orderedLetters.replace(0, 1, word.substring(0, 1));
    }
    //inserts the second letter of the word and moves the one it replaced out of the way
    if (isLetterSolved(1, 2)) {
        orderedLetters.replace(1, 2, word.substring(1, 2));
    }
    //inserts the third letter of the word and moves the one it replaced out of the way
    if (isLetterSolved(2, 3)) {
        orderedLetters.replace(2, 3, word.substring(2, 3));
    }
    //inserts the fourth letter of the word and moves the one it replaced out of the way
    if (isLetterSolved(3, 4)) {
        orderedLetters.replace(3, 4, word.substring(3, 4));
    }
    //inserts the fifth letter of the word and moves the one it replaced out of the way
    if (isLetterSolved(4, 5)) {
        orderedLetters.replace(4, 5, word.substring(4, 5));
    }
    lettersLabel.setText(orderedLetters.toString());
    System.out.println(userInput.getText().substring(0, 1));
    userInput.setText("");
} 

最佳答案

不完全相同的方式,但下面的代码使用不同的方法解决了问题。

创建 DTO。

public class WordDTO {

    private String orignalWord;

    private String newWord;

    private String guessdWord;

    private char inputChar;

    private int position;

    private boolean status;

    public String getNewWord() {
        return newWord;
    }

    public void setNewWord(String newWord) {
        this.newWord = newWord;
    }

    public String getGuessdWord() {
        return guessdWord;
    }

    public void setGuessdWord(String guessdWord) {
        this.guessdWord = guessdWord;
    }

    public char getInputChar() {
        return inputChar;
    }

    public void setInputChar(char inputChar) {
        this.inputChar = inputChar;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getOrignalWord() {
        return orignalWord;
    }

    public void setOrignalWord(String orignalWord) {
        this.orignalWord = orignalWord;
    }


}

该类接受用户输入并生成随机单词,然后要求用户猜测该单词。 我已经用 012345 初始化了初始猜测的单词,即从 0 到单词的大小。您可以使用“_”进行初始化

public class GuessTheWordMain {

    public static void main(String args[]) {
        Scanner reader=new Scanner(System.in);
        System.out.println("Enter the size of Word to guess!!!!");
        int sizeOfNum=reader.nextInt();
        String word=generateRndomWord(sizeOfNum);
        System.out.println(word);
        WordDTO wordDTO=new WordDTO();
        wordDTO.setNewWord(word);
        String guesWordInitial="";
        for (int j=0;j<word.length();j++) {
            guesWordInitial=guesWordInitial+String.valueOf(j);
        }
        wordDTO.setGuessdWord(guesWordInitial);
        wordDTO.setPosition(-1);
        for(int i=0;i<sizeOfNum;i++) {
            System.out.println("Enter the Guess Number :"+i);
            char guess=reader.next().charAt(0);
            wordDTO.setInputChar(guess);
            wordDTO=displayGuessResult(wordDTO);

            System.out.println(wordDTO.getGuessdWord());
        }
        System.out.println();

    }

    public static String generateRndomWord(int size) {
        Random random=new Random();
        StringBuilder stringBuilder=new StringBuilder(size);
        for(int i=0;i<size;i++) {
            stringBuilder.append((char)('a'+random.nextInt(26)));
        }
        return stringBuilder.toString();
    }

    public static WordDTO displayGuessResult(WordDTO wordDTO) {
        String newWord=wordDTO.getNewWord();
        boolean status=wordDTO.isStatus();
        int position=-1;
        for (int i=0;i<newWord.length();i++) {

            if((newWord.charAt(i))==wordDTO.getInputChar()) {
                status=true;
                position=i;
                break;
            }
        }
        if(status) {
            wordDTO.setNewWord(newWord.replace(newWord.charAt(position), '*'));
            wordDTO.setStatus(status);
            wordDTO.setGuessdWord(wordDTO.getGuessdWord().replace(wordDTO.getGuessdWord().charAt(position), wordDTO.getInputChar()));
        }
        return wordDTO;


    }
}

如有任何疑问/改进,请询问。

关于java - 字母未正确插入到 StringBuilder 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36486491/

相关文章:

java - 如何防止创建模拟对象来运行静态初始化?

java - Java从Excel中获取文本框的值

Java StringWriter toString 处的 StringBuilder 异常

具有干净输出的 ASP.NET HTML 控件?

java - 在 JAXB XmlAdapter 中执行 JNDI 查找

Java MigLayout问题

Java 8 Base64 编码(基本)不再添加新行。我怎样才能重新实现这个?

php - 如何优化通过 php/android 从 mysql 获取数据?

java - 我需要授予 java 应用程序 super 用户访问权限以查看 Mac 上 protected 文件

java - StringBuilder逆向方法的时间复杂度