Java charAt 的“赋值的左侧必须是变量”问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3639695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"The left-hand side of an assignment must be a variable" problem with charAt
提问by javaN00b
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) part points the problem in the title. How can I solve the problem, if my approach is not completely wrong.
state.charAt(i) 部分指出了标题中的问题。如果我的方法不是完全错误,我该如何解决问题。
采纳答案by Nikita Rybak
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.
这不起作用的原因是因为charAt(int x)
是String
类的方法- 即它是一个函数,并且您不能在 Java 中为函数赋值。
If you want to loop through a string character by character, I might be tempted to do this:
如果您想逐个字符地遍历字符串,我可能会尝试这样做:
Char[] GuessAsChar = guess.toCharArray();
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.
然后改为对 GuessAsChar 进行操作。根据您的需要,可能有更好的(如更简洁的)方法来搜索字符串中的字符等价性。
回答by Mark Byers
Strings are immutablein Java. This means that you cannot change a string object once you have created it. You can however create a new string and then reassign it to the variable.
字符串在 Java 中是不可变的。这意味着一旦创建了字符串对象,就无法更改它。但是,您可以创建一个新字符串,然后将其重新分配给变量。
state = state.substring(0, i) + word.charAt(i) + state.substring(i + 1);
However in this situation I think it would be better to use a mutable type for state
such as a character array (char[]
). This allows you to modify individual characters directly.
但是在这种情况下,我认为最好使用可变类型,state
例如字符数组 ( char[]
)。这允许您直接修改单个字符。
A second problem with your code is that guess
should presumably be a char, not a string. Currently your if
statement will always return false because an object of type string
will never be equal to an object of type char
.
您的代码的第二个问题是guess
大概应该是一个字符,而不是一个字符串。当前,您的if
语句将始终返回 false,因为 type 的对象 string
永远不会等于 type 的对象char
。
回答by Nikita Rybak
Strings in Java are immutable: you can't change string after it's created. It might be better to use byte[]
or char[]
or collection for state
.
Java 中的字符串是不可变的:字符串创建后不能更改。使用byte[]
orchar[]
或 collection for可能更好state
。
回答by David Blevins
Not exactly sure what the intention is for guess.equals(word.charAt(i))
as that statement will always evaluate to false since a String
never can equal a char
, but you want to convert your String
to a StringBuilder
不完全确定意图是什么,guess.equals(word.charAt(i))
因为该语句将始终评估为 false,因为 aString
永远不能等于 a char
,但您想将您的转换String
为 aStringBuilder
private String kNow(String state, String guess) {
final StringBuilder mutable = new StringBuilder(state);
for (int i = 0; i < word.length(); i++) {
if (guess.equals(word.charAt(i))) {
mutable.setCharAt(i, word.charAt(i));
}
}
return mutable.toString();
}