Java输出错误字符串处理

收割者1

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

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

我找不到错误。

ole

您有不正确的for循环条件。将“ i <= a”更改为“ i <a”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章