在for循环上,我有Java applet向我显示我有一个错误。我正在尝试使用for循环来计算字母的重复。
String countString = "";
for (int i = 0; i < 26; i++){
// at the line below, my java applet says I have an error, and that the
//"letterCounts" should be a int and not a string, but I need it to be a string
String n = letterCounts[i];
if (n.equals("0")) {
countString = countString + " ";
} else if (n.length() == 1) {
countString = countString + " " + n + " ";
} else {
countString = countString + n + " ";
}
}
this.countLabel.setText(countString);
最佳答案
您没有显示letterCounts
的定义,但我敢打赌它是int[] letterCounts
。
因此,由于letterCounts
是int
的数组,您不能仅将其分配给String
。
只需将String n
更改为int n
,然后将其与n == 0
的比较就可以了。见下文:
String countString = "";
for (int i = 0; i < 26; i++)
{
int n = letterCounts[i];
if (n == 0) {
countString = countString + " ";
} else if (n < 10) {
countString = countString + " " + n + " ";
} else {
countString = countString + n + " ";
}
}
this.countLabel.setText(countString);