Java输出错误字符串操作

时间:2015-01-10 09:05:04

标签: java

我有这段代码来计算字符串中相似字符的连续字符的次数。

public class Solution {

public static  int count;

public static void main(String[] args) 
{

    Scanner s= new Scanner(System.in);
    int a = s.nextInt();//no of testcases
    char a1,b1;
    for(int i=0;i<=a;i++)
    {
        String str=s.nextLine();
        int len=str.length();
        for(int b=0;b<len-1;b++)
        { 
            a1=str.charAt(b);           
            b1=str.charAt(b+1);
            if(a1==b1) count++;               
        }
        System.out.println(count);
        count=0;
    }
}
}

输入:

1
AAAA

预期产出:

3

节目输出:

0
3

我找不到错误。

1 个答案:

答案 0 :(得分:1)

您的for循环条件不正确。改变&#34; i&lt; = a&#34;到&#34; i&lt;一个&#34;