我正在编码hangman class,并且unexpected type error有点问题。 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 - 子手类发生意外的类型错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28010328/

10-11 22:09