簡體   English   中英

計算Java數組中的大小寫字符

[英]Count upper and lower case characters in Java array

我已經弄清楚了如何遍歷數組並計算每個字母並返回。 我通過將其與其ascii值進行比較來做到這一點。 我的問題是,我也無法使其采用大寫形式。 如何設置它還計算大寫字母?

package test;

import java.util.Arrays;

public class arrays {
    public static void array(char[] charAlphabet) {
        int intCount = 97;
        int intNumberof = 0;
        //sort the array into alphabetical order
        Arrays.sort(charAlphabet);
        //look at each character
        for(int x = 0; x < charAlphabet.length;) {
            //if the position of the character is equal to its place in the alphabet, increase the count
            if(charAlphabet[x] == intCount) {
                intNumberof++;
                x++;
            }
            //when it reaches the end of the letters, print it
            else {
                intCount++;
                //print the letter and the number of letters
                System.out.println(Character.toString(charAlphabet[x -1]) + " x " + intNumberof);
                intNumberof = 0;
            }
        }
    }

    //input
    public static void main(String[] dfsgsdg) {
        char[] charAlphabet = "aabcdefghijklmnoapqrstuvwxyaz".toCharArray();
        array(charAlphabet);

    }
}

- 編輯 -

剛剛意識到實際上我的邏輯有點錯,因為它實際上錯過了數組中的最后一個字符,但是如果我刪除打印中的-1,則實際上錯過了第一個字符。

嘗試使用“ ABa”,它將找不到兩倍於intNumberof++ “ a”(97)。

對於“ a22c”也是如此。

忘記排序,檢查charAlphabet[x].isUpperCase()每個字母。 isLowerCase()

您可以將每個字符作為字符串,然后應用此邏輯。

String temp= str;
If(str.toUpperCase().equals(temp)){
  //It is uppercase
}
else{
//lower case
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM