繁体   English   中英

检查一个字符连续出现在字符串中的次数

[英]Check how many times a character occurs consecutively in a String

我试图计算一个字符在字符串中连续出现的次数。 :\\如果您能帮助我,我将不胜感激。

提前致谢 :)

        int totCharacters=0, vowels=0, digits=0, odds=0, consecutive=0, index=0;
        String text;
        char ch;

        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a sentence terminated with a full stop or exclamation mark: ");
        text = input.nextLine();

        do {
            ch = text.charAt(index);

            if(ch=='.' && ch=='!')
                break;

            if(index<text.length()-1)
                totCharacters++;
            if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
                vowels++;
            if(ch>='0' && ch<='9')
                digits++;
            if(ch=='1' || ch=='3' || ch=='5' || ch=='7' || ch=='9')
                odds++;

            index++;

        }while(ch!= '.' && ch!='!');

        for(int i=0; i<text.length(); i++)
        {

            if(ch==text.charAt(i))
                consecutive++;

        }

我假设问题在于它没有按您期望的方式consecutive计数。 问题在于,遍历所有字符后, for循环正在运行。 由于ch是最后一次设置为'。'时设置的。 要么 '!' 遇到时,for循环只是计数所有的'。 要么 '!' 字符。 不仅是连续的。

我会做这样的事情。 有两根琴弦,一个装有所有辅音,另一个装有所有元音。 检查字符串之一是否包含字符。 如果不是,则为标点符号或空格。 代码过滤掉了空间。

String consString = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
String vowelString = "aeiouAEIOU";

for (int i = 0; i < s.length(); i++){
    if (consString.contains(s.charAt(i)){
        consCount++;
    }
    else if (vowelString.contains(s.charAt(i)){
        vowelCount++;
    } 
    else if (!Character.isWhiteSpace(s.charAt(i))
        punctCount++;
    }
}

您可以为所需的任何字符集执行此操作。 另外,如果要计数连续数,则可以保留currentChar变量,并检查下一个变量是否等于它。 如果有,则consecutives++

这将计算所有连续的字符(我相信不能编译atm)。 这可能不是您想要的。 我可以根据连续字符的含义进行修改。 全部连续? 最高连续?

我将for循环移至do while内,以便您对每个字符进行处理。 我还将基于索引的int i变量作为基础,以便您不会在正在检查的当前字符之前查看字符。 也将索引的增量移动到for循环之后,以使其自身算作连续一个,因此“ AA”将连续2个。

我需要您定义连续的次数,以使其正确计算。 现在,它将查看每个字符,并为每个匹配的字符在其自身(包括自身)之后添加1。

    int totCharacters=0, vowels=0, digits=0, odds=0, consecutive=0, index=0;
    String text;
    char ch;

    Scanner input = new Scanner(System.in);
    System.out.print("Please enter a sentence terminated with a full stop or exclamation mark: ");
    text = input.nextLine();

    do {
        ch = text.charAt(index);

        if(ch=='.' && ch=='!')
            break;

        if(index<text.length()-1)
            totCharacters++;
        if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
            vowels++;
        if(ch>='0' && ch<='9')
            digits++;
        if(ch=='1' || ch=='3' || ch=='5' || ch=='7' || ch=='9')
            odds++;


    for(int i=index; i<text.length(); i++)
        {

             if(ch==text.charAt(i))
             {
                consecutive++;
             }
             else 
             {
                 break;
             }

        }
        index++;
        }while(ch!= '.' && ch!='!');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM