簡體   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