如何使用switch循环计算字符串中的多个字符出现次数? Java的

时间:2015-03-06 04:26:29

标签: java

想要计算字母a,e,s,t也是大写字母。使用开关循环。我已经拥有它所以它计算空白但我不知道如何设置开关以及如何打印它。谢谢

import java.util.Scanner;

public class Count {
    public static void main(String[] args) {
        String phrase;   // a string of characters
        int countBlank;  // the number of blanks (spaces) in the phrase
        int length;      // the length of the phrase
        char ch;         // an individual character in the string

        Scanner scan = new Scanner(System.in);
        // Print a program header
        System.out.println();
        System.out.println("Character Counter");
        System.out.println();
        // Read in a string and find its length
        System.out.print("Enter a sentence or phrase: ");
        phrase = scan.nextLine();
        length = phrase.length();
        // Initialize counts
        countBlank = 0;
        // a for loop to go through the string character by character
        // and count the blank spaces
        // Print the results System.out.println ();
        for (int i = 0; i < phrase.length(); i++) {
            if (Character.isWhitespace(phrase.charAt(i))) {
                countBlank++;
            }
        }
        ch = phrase.charAt(phrase.length());

        System.out.println();

        System.out.println("Number of blank spaces: " + countBlank);

        System.out.println();
    }
}

4 个答案:

答案 0 :(得分:0)

我认为这可能会有所帮助,

import java.util.Scanner;
class Count {
    public static void main(String[] args) {
        String phrase;   // a string of characters
        int countBlank;  // the number of blanks (spaces) in the phrase
        int length;      // the length of the phrase
        char ch;         // an individual character in the string


        phrase = "abcd efgh skjadh sjds";
        length = phrase.length();
        int i = 0;
        // Initialize counts
        countBlank = 0;
        // a for loop to go through the string character by character
        // and count the blank spaces
        // Print the results System.out.println ();
        int aCount = 0;
        int eCount = 0;
        int sCount = 0;
        int tCount = 0;
        int capACount = 0;
        int capECount = 0;
        int capSCount = 0;
        int capTCount = 0;
        while(i < length) {
            ch = phrase.charAt(i);
            switch(ch) {
                case 'a' :
                    aCount++;
                    break;
                case 'e' : 
                    eCount++;
                    break;
                case 's' :
                    sCount++;
                    break;
                case 't' : 
                    tCount++;
                    break;
                case 'A' :
                    capACount++;
                    break;
                case 'E' :
                    capECount++;
                    break;
                case 'S' :
                    capSCount++;
                    break;
                case 'T' :
                    capTCount++;
                    break;

            }
            i++;
        }

        System.out.println("Number of a: " + aCount);
        System.out.println("Number of e: " + eCount);
        System.out.println("Number of s: " + sCount);
        System.out.println("Number of t: " + tCount);
        System.out.println("Number of A: " + capACount);
        System.out.println("Number of E: " + capECount);
        System.out.println("Number of S: " + capSCount);
        System.out.println("Number of T: " + capTCount);

    }
}

答案 1 :(得分:0)

如何使用JDK:

int countBlank = phrase.length() - phrase.replace(" ", "").length();
int countAest = phrase.length() - phrase.replaceAll("(?i)[aest]", "").length();

这两行通过移除感兴趣的字符然后使用长度变化来计算。

仅供参考,在正则表达式中,(?i)表示“忽略大小写”,[aest]表示“任何字符a,e,s或t”。

答案 2 :(得分:0)

考虑将您要查找的字符也放在String中。这样,您可以在输入charAt()上使用phrase方法以及要查找的字符串。

    public static void main(String[] args) {
        // Print a program header
        System.out.println();
        System.out.println("Character Counter");
        System.out.println();

        // Read in a string and find its length
        System.out.print("Enter a sentence or phrase: ");
        Scanner scan = new Scanner(System.in);
        String phrase = scan.nextLine();
        int length = phrase.length();

        // Initialize counts
        String findChars = " AaEeSsTt"; // we will count each of these characters
        int[] counts = new int[findChars.length()]; // the counts of findChars

        // Go through the string character by character and count findChars
        for (int i = 0; i < length; i++) {
            char ch = phrase.charAt(i);
            for (int n = 0; n < findChars.length(); n++) {
                char find = findChars.charAt(n);
                if (ch == find) {
                    counts[n]++;
                    break;
                }
            }
        }
        System.out.println();

        // Print the results to System.out
        for (int n = 0; n < findChars.length(); n++) {
            System.out.println("Number of '"
                + findChars.charAt(n) + "': " + counts[n]);
        }
        System.out.println();
    }

答案 3 :(得分:0)

在下面的示例中,首先我们将一个句子作为String,我们将按Character阅读Character,当我们阅读每个Character时,我们将增加(相应的索引值),int Array,如:

int value = ( int ) sentence.charAt (i);
characterCount [value]++;

如果只有您知道,您正在阅读ASCII字符集,那么您也可以使用此方法,或者修改它以满足项目的需要。

public class AlphabetCountingExample {

    private static final int TOTAL_ASCII_CHARACTERS = 256;
    private String sentence;
    private int [] characterCount;

    public AlphabetCountingExample () {
        characterCount = new int [TOTAL_ASCII_CHARACTERS];
        sentence = "Hello there!!!";
    }

    private void performTask () {
        countCharacters ();     
        displayCount ();
    }

    private void countCharacters () {
        for ( int i = 0; i < sentence.length (); ++i ) {
            int value = ( int ) sentence.charAt (i);
            characterCount [value]++;
        }
    }

    private void displayCount () {
        for ( int i = 0; i < characterCount.length; ++i ) {
            if ( characterCount [ i ] > 0 ) {
                System.out.println ( "Character: " + (char) i +
                                " Count: " + characterCount [ i ] );
            }
        }
    }

    public static void main (String[] args) {
        new AlphabetCountingExample ().performTask ();
    }
}