Java:特定字符报告的数量

时间:2015-02-25 23:46:20

标签: java string algorithm loops int

我制作了一个程序来计算短语中每个字母的数量

我希望返回的结果文本是

  

输入短语

     

"牛进入市场"

     

有1个C(' s),3个O(' s),1个W(' s),1个G(' s),3个E(&# 39; s),1 S(' s),3 T(' s).. / *你得到了你的短语中的要点*。

我希望将结果连接成一个字符串,而不仅仅是一个多行报告。

import java.util.*;

public class charCount 
{
  public static void main(String[] args)
  {
   Scanner keyboard = new Scanner (System.in);
    //Prompt for user entry
    System.out.println("Enter a phrase: ");
    String letterN = keyboard.nextLine();

    charCount(letterN);
  }


public static int charCount(String letterN)
{ //variable initializations 

    int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
    a = b = c = d = e = f = g = h = i = j = k = l = m = n = o = p = q = r = s = t = u = v = w = x = y = z = 0;

    int total = 0;
    total += 1;

//for loop 
for(int countN = 0; countN < letterN.length(); countN++) {
    switch(letterN.charAt(countN)) {
     case 'a':
        a++;
        break;
     case 'b':
        b++;
        break;
     case 'c':
    c++;
    break;
     case 'd':
    d++;
    break;
     case 'e':
    e++;
    break;
     case 'f':
    f++;
    break;
     case 'g':
        g++;
        break;
     case 'h':
        h++;
        break;
     case 'i':
    i++;
    break;
     case 'j':
    j++;
    break;
     case 'k':
    k++;
    break;
     case 'l':
    l++;
    break;
     case 'm':
        m++;
        break;
     case 'n':
        n++;
        break;
     case 'o':
    o++;
    break;
     case 'p':
    p++;
    break;
     case 'q':
    q++;
    break;
     case 'r':
    r++;
    break;
     case 's':
    s++;
    break;
     case 't':
    t++;
    break;
     case 'u':
    u++;
    break;
     case 'v':
    v++;
    break;
     case 'w':
    w++;
    break;
     case 'x':
    x++;
    break;
     case 'y':
    y++;
    break;
     case 'z':
    z++;
    break;

    }

}

    //Console logs
    System.out.println("Below are the individual character counts for your string");
    System.out.println("A: " + a);
    System.out.println("B: " + b);
    System.out.println("C: " + c);
    System.out.println("D: " + d);
    System.out.println("E: " + e);
    System.out.println("F: " + f);
    System.out.println("G: " + g);
    System.out.println("H: " + h);
    System.out.println("I: " + i);
    System.out.println("J: " + j);
    System.out.println("K: " + k);
    System.out.println("L: " + l);
    System.out.println("M: " + m);
    System.out.println("N: " + n);
    System.out.println("O: " + o);
    System.out.println("P: " + p);
    System.out.println("Q: " + q);
    System.out.println("R: " + r);
    System.out.println("S: " + s);
    System.out.println("T: " + t);
    System.out.println("U: " + u);
    System.out.println("V: " + v);
    System.out.println("W: " + w); 
    System.out.println("X: " + x);
    System.out.println("Y: " + y);
    System.out.println("Z: " + z);

    return total;    
  }  
}

我的问题是,我当然可以连接一个System.out.println();使用int vars但它只是打印字母数。我不会提前知道人们会输入什么,所以我需要我的程序来检测字母是否为真,从而返回报告中的字符和数字。

我寻找解决方案而找不到东西。我无法解决这个问题,坦率地说,我不知道怎么说这个问题。

提前谢谢你们

1 个答案:

答案 0 :(得分:1)

使用地图存储您遇到的字符以及看到它们的次数。使用TreeMap意味着当您遍历地图时,条目按自然顺序排序

import java.util.*;

public class CharCount {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        //Prompt for user entry
        System.out.println("Enter a phrase: ");
        String letterN = keyboard.nextLine();

        charCount(letterN);
    }


    public static void charCount(String letterN) {
        Map<Character, Long> charMap = new TreeMap<>();
        for (char aChar : letterN.toCharArray()) {
            if (charMap.containsKey(aChar)) {
                charMap.put(aChar, charMap.get(aChar) + 1);
            }
            else {
                charMap.put(aChar, 1L);
            }
            // this if/else can be written using the ternary operator as:
            // charMap.put(aChar, charMap.containsKey(aChar) ? charMap.get(aChar) + 1 : 1L);
        }
        for (Character character : charMap.keySet()) {
            System.out.println(character + ": " + charMap.get(character));
        }
    }
}