【发布时间】:2013-10-10 20:43:40
【问题描述】:
我需要找出数字 1 在诸如“11100011”之类的字符串中出现的次数 这样我就可以使用计数来做一些奇偶校验位工作。我想知道是否有人可以告诉我什么方法或如何设置循环来做这样的事情。
public class message
{
private String parity1;
private int count;
public message(String Parity1)
{
parity1 = Parity1;
int count = 0;
}
public static int countOnes(String parity1, char 1)
{
count = 0;
for(int i = 0; i < parity1.length(); i++) {
if(parity1.charAt(i)==1){
count++;
}
}
return count;
}
//...
【问题讨论】:
-
blah blah blah 你试过什么 blah blah blah(IOW,阅读常见问题解答)
-
使用
for循环并在字符串上调用charAt。 -
让我们看看你目前拥有什么。
-
字符串 ar = "111000011"; int countOfOnes = ar.length() - ar.replaceAll("1", "").length();
-
这就是我得到的 char 1 的标识符预期错误