我已经完成了创建一个方法的任务,该方法接受一个char并进行相等性检查以查看它是否与数组中的任何char匹配。
对于找到匹配的次数,计数器应该上升。我很确定for循环的语法是正确的,但我不知道如何运行相等性检查。
if(tiles.toCharArray()==letter)
是我目前的尝试。有关如何切换或更改此行代码以使相等测试工作的任何想法?
public class ScrabblePlayer {
private String tiles;
int count;
// A String representing all of the tiles that this player has
public char ScrabblePlayer() {
tiles = "";
}
public int getCountOfLetter(char letter) {
count = 0;
for(char character : tiles.toCharArray()) {
if(tiles.toCharArray() == letter);
count += 1;
}
return count;
}
答案 0 :(得分:0)
应该是这样的: -
if(character == letter) {
count += 1;
}
在旁注中,我认为public char ScrabblePlayer()
应该是public ScrabblePlayer()
,如果它应该是构造函数。
答案 1 :(得分:0)
在getCountOfLetter
方法代码中,您有两个问题:
;
条件行末尾的无用分号if
。if
条件中使用了错误的变量,tiles.toCharArray()
应该替换为character
,因为您正在循环它。这应该是你的代码:
public int getCountOfLetter(char letter) {
count = 0;
for(char character : tiles.toCharArray()) {
if(character == letter)
count ++;
}
return count;
}