0

文字列の出力を印刷して、大文字と小文字の数を見つけようとしています。

たとえば、string = "AaaBBbCc" の場合、"A1a2B2b1C1c1" として出力する必要があります。

IE 大文字の「A」のカウント、次に小文字の「a」のカウント、文字の追加。

以下は、私が行ったところまでのコードスニペットです。誰でもそれがどうなるかを提案できますか。私はコードがマークに達していないことを知っています:(

public static void main(String[] args) {
    String str = "AaaBBbCc";
    int upperCount=0;
    int lowerCount=0;

    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if(ch>='A' && ch<='Z'){
             upperCount++;
             System.out.println("Uppercase letter is : "+ch+upperCount);

    }
     if(ch>='a' && ch<='z'){
        lowerCount++;
        System.out.println("Lower case letter is : "+ch+lowerCount);
    }
}
    System.out.println("upper count is :"+upperCount+" & lower count is: "+lowerCount);     

}

4

2 に答える 2

0

あなたは正しい軌道に乗っています。大文字か小文字かだけでなく、どの文字が表示されるかを数えたい場合は、2 つのint[]配列upperCaseCountと を作成できますlowerCaseCount = new int[26]。これらの配列を使用して、表示される文字を数えることができます。

インクリメントする必要があるインデックスを決定するため charに使用できるという事実を利用できるヒント:int

int index = ? //'a' should be 0 for lower, and 'A' should be 0 for upper
lowerCaseCount[index]++ or upperCaseCount[index]++; 
于 2016-03-14T11:41:31.520 に答える