Java:特定字符报告的数量

标签 java string algorithm loops int

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

我希望结果文本返回的是

Enter a phrase

"Cow goes to the market"

There are 1 C('s), 3 O('s), 1 W('s), 1 G('s), 3 E('s), 1 S('s), 3 T('s).. /* you get the gist */ in your phrase.

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

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 但它只是打印字母数。我事先不知道人们会输入什么,所以我需要我的程序检测字母是否 = true,从而返返回告中的字符和数字。

我一直在寻找解决方案,但一无所获。我还不能全神贯注,坦率地说,我不知道如何表达这个问题。

提前谢谢大家

最佳答案

用一个Map来存储你遇到过的人物,以及他们见过的次数。使用 TreeMap 意味着当您遍历 map 时,条目将按自然顺序排序

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));
        }
    }
}

关于Java:特定字符报告的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731972/

相关文章:

algorithm - 找到一个数在达到 1 之前可以被 2 除多少次的最有效算法 [注 : if possible in O(1) time]

java - Java的BufferunderflowException

java - 如何从模块(Android)继承应用程序依赖

java - 在调用 getResource() 之前,我是否需要在 getClassLoader() 上处理 null

C : Access violation reading location 0x00000044

c# - 是否可以使用 String.Split 或正则表达式将字符串拆分为字符串数组并删除不在分隔符之间的部分?

python - 词表生成(排序、优化)

java - Hadoop:java.lang.Exception:java.lang.RuntimeException:配置对象时出错

javascript - 如何从某个地方开始拆分字符串?

java - 快速排序 Java 。 ArrayIndexoutofBoundsException异常