gpt4 book ai didi

java - Hangman 类中出现意外的类型错误

转载 作者:行者123 更新时间:2023-12-01 18:19:21 25 4
gpt4 key购买 nike

我正在编写一个hangman 类,并且遇到了一个意外类型错误的小问题。此错误位于 this.hidden.charAt(i) = this.original.charAt(i); 中。任何帮助将不胜感激。

public class HangmanClass {


private ArrayList<String> words = new ArrayList();
private String word;
private final static char HIDECHAR = '*';


private String original;
private String hidden;

public void HangmanWord(String original){
this.original = original;
this.hidden = this.hideWord();
}

public String getWord() {

words.add("greetings");
words.add("cheese");
words.add("making");
words.add("juvenile");
words.add("car");
word = words.get(new Random().nextInt(words.size()));
return word;

}

private String hideWord() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
sb.append(HIDECHAR);
}
return sb.toString();
}

public boolean checkInput(char input){
boolean found = false;
for (int i = 0; i < this.original.length(); i++) {
if (this.original.charAt(i) == input) {
found = true;
this.hidden.charAt(i) = this.original.charAt(i);
}
}
return found;

}


};

最佳答案

String.charAt(int) 不能用在赋值语句的左侧。并且您无法更改/改变 Java 中的 String

改用StringBuilder.setCharAt(int index, char ch)

关于java - Hangman 类中出现意外的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28010328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com