gpt4 book ai didi

java - 如何统计数组中所有字母字符的个数?

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:30 25 4
gpt4 key购买 nike

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

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

}

那么我如何跟踪用户输入的所有字母?

最佳答案

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

您可以使用 HashMap 来存储字符和整数。键/值对每个映射的每个键只有一个条目。示例

关于java - 如何统计数组中所有字母字符的个数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27133796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com