“赋值的左侧必须是变量” charAt的问题 [英] "The left-hand side of an assignment must be a variable" problem with charAt

查看:245
本文介绍了“赋值的左侧必须是变量” charAt的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private String kNow(String state, String guess) {
        for (int i = 0; i < word.length(); i++) {
            if (guess.equals(word.charAt(i))) {
                state.charAt(i) = word.charAt(i);
            }
        }
        return state;
    }

state.charAt(i)部分指出标题中的问题。
如果我的方法不完全错误,我该如何解决问题。

state.charAt(i) part points the problem in the title. How can I solve the problem, if my approach is not completely wrong.

推荐答案

工作是因为 charAt(int x) String 类的一个方法 - 即它是一个函数,不能在Java中为一个函数赋值。

The reason this doesn't work is because charAt(int x) is a method of the String class - namely it is a function, and you can't assign a function a value in Java.

如果你想循环一个字符串字符,我可能会这样做:

If you want to loop through a string character by character, I might be tempted to do this:

Char[] GuessAsChar = guess.toCharArray();

然后在GuessAsChar上操作。有些,根据你的需要,可能更好(如在更干净的)方法搜索字符等价在字符串。

Then operate on GuessAsChar instead. There are, depending on your needs, possibly better (as in neater) ways to approach searching for character equivalence in strings.

这篇关于“赋值的左侧必须是变量” charAt的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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