Java 如何计算数组中的所有字母字符?

Java 如何计算数组中的所有字母字符?,java,counter,nested-loops,Java,Counter,Nested Loops,所以基本上我的目标是制作一个程序,接收用户输入并反转它,然后将反转字符作为编码信息打印回用户。现在我需要打印用户输入字符串的统计信息。 如何计算用户字符串中不同字母的出现次数。到目前为止,我有这个 import java.util.*; public class SecretCodeMachine { public static void main(String[]args) { //object accessing the non static methods

所以基本上我的目标是制作一个程序,接收用户输入并反转它,然后将反转字符作为编码信息打印回用户。现在我需要打印用户输入字符串的统计信息。 如何计算用户字符串中不同字母的出现次数。到目前为止,我有这个

   import java.util.*;

  public class SecretCodeMachine
 {
  public static void main(String[]args)
 {
    //object accessing the non static methods
    SecretCodeMachine a = new SecretCodeMachine();

    //input stream
    Scanner in = new Scanner (System.in);
    Scanner i = new Scanner (System.in);

    //prompt the user
    System.out.println("Please input your secret message.");
    String input = in.nextLine();

    //calls the encodedMessage() method; equals the return value to varaible
    String encodedMessage = a.encodeMessage(input);

    //message and prompt
    System.out.println("Encoded message: " + encodedMessage);
    System.out.println("Enter the code in here to get the original message back.");
    String input2 = i.nextLine();

    //if statements saying that if the input equals the encoed message...
    if (input2.equals(encodedMessage))
    {
        //print this
        System.out.println("Original Message: " + input);
    }
    else
    {
        //prints when doesnt equal
        System.out.println("Message not found.");
    }

    //closes the input stream
    i.close();
    in.close();

}
//method for encoding the string from array
public String encodeMessage(String pass)
{
    //passes the parameter string and puts it in an array ()
    char[] toArray = pass.toCharArray();

    for (int i = 0; i < toArray.length; i++) 
    {
        //does the lower case characters
        if (toArray[i] >= 'a' && toArray[i] <= 'z') 
        {
            if (toArray[i] - 'a' <= 13) toArray[i] = (char) ('z' - (toArray[i] - 'a'));
            else toArray[i] = (char) ('a' + ('z' - toArray[i]));
        }

        //does the upper case characters
        else if(toArray[i] >= 'A' && toArray[i] <= 'Z')
        {
            if (toArray[i] - 'A' <= 13) toArray[i] = (char) ('Z' - (toArray[i] - 'A'));
            else toArray[i] = (char) ('A' + ('Z' - toArray[i]));
        }
        //if the characters are non alphatbetic 
        else 
        {
            toArray[i] = toArray[i];
        }
    }

    //converts the toArray back to new string 
    String encodedMessage = new String(toArray);

    //returns the encodedMessage string
    return encodedMessage;
}
import java.util.*;
公共类加密机
{
公共静态void main(字符串[]args)
{
//对象访问非静态方法
SecretCodeMachine a=新的SecretCodeMachine();
//输入流
扫描仪输入=新扫描仪(系统输入);
扫描器i=新扫描器(System.in);
//提示用户
System.out.println(“请输入您的秘密消息”);
字符串输入=in.nextLine();
//调用encodedMessage()方法;等于varaible的返回值
字符串encodedMessage=a.encodeMessage(输入);
//信息和提示
System.out.println(“编码消息:“+encodedMessage”);
System.out.println(“在此处输入代码以获取原始消息”);
字符串input2=i.nextLine();
//如果语句表明如果输入等于编码消息。。。
if(input2.equals(encodedMessage))
{
//打印这个
System.out.println(“原始消息:”+输入);
}
其他的
{
//不相等时打印
System.out.println(“未找到消息”);
}
//关闭输入流
i、 close();
in.close();
}
//方法对数组中的字符串进行编码
公共字符串编码消息(字符串传递)
{
//传递参数字符串并将其放入数组()
char[]toArray=pass.toCharray();
for(int i=0;i如果(toArray[i]>='a'&&toArray[i]维护一个键为字符、计数为整数的映射。下面是可以帮助您的方法:

public Map<Character, Integer> getCharCount(String input) {
  Map<Character, Integer> charCountMap = new HashMap<Character, Integer>();
  Integer count;
  for (int i =0; i<input.length();i++) {
    count = charCountMap.get(input.charAt(i));
    if (count == null) {
      charCountMap.put(input.charAt(i), 1);
    } else {
      charCountMap.put(input.charAt(i), count + 1);
    } 
  }
  return charCountMap;
}
publicmap getCharCount(字符串输入){
Map charCountMap=newhashmap();
整数计数;
对于(int i=0;i

if(toArray[i]>='a'&&toArray[i]这里有一个策略:1)将用户在
集合中输入的所有字符放入
中。2)维护
集合中每个字符的
映射图
,并将其频率(您即将发现)初始化为零。3)迭代用户输入字符串中的每个字符,并增加
映射中的字符频率(如果在
集合中找到)。可以使用hashmap示例
 public class SecretCodeMachine
 {
     HashMap<Character, Integer> charCounts = new HashMap<Character, Integer>();//add hashmap here
for (int i = 0; i < toArray.length; i++) 
    {
        //does the lower case characters
        //update count for character
        if(charCounts.get(toArray[i]) != null)
        {
            charCounts.put(toArray[i], (charCounts.get(toArray[i]) + 1));
        }
        else
        {
            charCounts.put(toArray[i], 1);
        }
        if (toArray[i] >= 'a' && toArray[i] <= 'z') 
        {
            if (toArray[i] - 'a' <= 13) toArray[i] = (char) ('z' - (toArray[i] - 'a'));
            else toArray[i] = (char) ('a' + ('z' - toArray[i]));
        }

        //does the upper case characters
        else if(toArray[i] >= 'A' && toArray[i] <= 'Z')
        {
            if (toArray[i] - 'A' <= 13) toArray[i] = (char) ('Z' - (toArray[i] - 'A'));
            else toArray[i] = (char) ('A' + ('Z' - toArray[i]));
        }
        //if the characters are non alphatbetic 
        else 
        {
            toArray[i] = toArray[i];
        }
    }