gpt4 book ai didi

java - 如何在Java中对随机字母数组求和?

转载 作者:行者123 更新时间:2023-12-02 02:45:47 25 4
gpt4 key购买 nike

我想问是否有什么好的算法可以对随机字符进行求和。

我期待 Cmd = 上的输出

Random lowercase letters are  : 

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

Iteration every characters :

a : 5, b : 3, c : 2, d : 1, e : 3, f : 6, g : 4, i : 4, j : 5,
k : 5, l : 6, m : 1, n : 2, o : 11, p : 2, q : 3, r : 3, s : 4, t : 3,
u : 4, v : 4, w : 3, x : 5, y : 3, z : 4

这是我的源代码=

public class CountRandomLettersArray {
/** Main method */
public static void main(String[] args) {
//declare and create an array
char[] kar = createArray();

//show lowercase character array
System.out.println("Random lowercase letters are : ");
System.out.println();
showArray(kar);

//counting every character loop
int[] count = countLetter(kar);

//show sum of every character iteration
System.out.println();
System.out.println("Iteration every characters : ");
System.out.println();
showCount(count);
}

/** counting every character loop method */
public static int[] countLetter(char[] kar) {
//declare and create an array that holds 26 of integer data type (26 means sum of 'a' to 'z')
int[] count = new int[26];

//count the loop for every lowercase letter in kar[] array
for (int i = 0; i < kar.length; i++) {
if (kar[i] == 'a')
count[0]++;
if (kar[i] == 'b')
count[1]++;
if (kar[i] == 'c')
count[2]++;
if (kar[i] == 'd')
count[3]++;
if (kar[i] == 'e')
count[4]++;
if (kar[i] == 'f')
count[5]++;
if (kar[i] == 'g')
count[6]++;
if (kar[i] == 'h')
count[7]++;
if (kar[i] == 'i')
count[8]++;
if (kar[i] == 'j')
count[9]++;
if (kar[i] == 'k')
count[10]++;
if (kar[i] == 'l')
count[11]++;
if (kar[i] == 'm')
count[12]++;
if (kar[i] == 'n')
count[13]++;
if (kar[i] == 'o')
count[14]++;
if (kar[i] == 'p')
count[15]++;
if (kar[i] == 'q')
count[16]++;
if (kar[i] == 'r')
count[17]++;
if (kar[i] == 's')
count[18]++;
if (kar[i] == 't')
count[19]++;
if (kar[i] == 'u')
count[20]++;
if (kar[i] == 'v')
count[21]++;
if (kar[i] == 'w')
count[22]++;
if (kar[i] == 'x')
count[23]++;
if (kar[i] == 'y')
count[24]++;
if (kar[i] == 'z')
count[25]++;
}

return count;
}

/** generate a random array of lowercase letters method*/
public static char[] createArray () {
//declare and create an array of 100 chars
char[] kar = new char[100];

//generate lowercase letters
for (int i = 0; i < kar.length; i++)
kar[i] = RandomCharacters.getLowercase();

return kar;
}

/** show characters array method*/
public static void showArray (char[] kar) {
//shows five lines of lowercase letters
final int CHARACTERS_PER_LINE = 20;
for (int i = 0; i < kar.length; i++) {
if ((i + 1) % CHARACTERS_PER_LINE == 0)
System.out.println(kar[i]);
else
System.out.print(kar[i] + " ");
}
}


/** show sum of characters iteration method*/
public static void showCount(int[] count) {
final int SUM_OF_CHARACTERS_PER_LINE = 10;
for (int i = 0; i < count.length; i++) {
if ((i + 1) % SUM_OF_CHARACTERS_PER_LINE == 0)
System.out.println((char) (i + 'a') + " : " + count[i] + ", ");
else
System.out.print((char) (i + 'a') + " : " + count[i] + ", ");
}
}
}

但它给了我这个错误:Compiler error

让我困惑的是为什么 int 不能转换为 count[] 数组上的 int[] (第 13 行),而 char 可以放入 kar[] 数组上的 char[] 中。因此 kar[] 数组旨在仅显示随机小写字母,而 count[] 数组旨在总结 kar[] 数组具有的每个字符。

顺便说一句,这里是 RandomCharacters.getLowercase() --> 第 93 行的源代码:

public class RandomCharacters {
/** Generate random characters from ch1 to ch2 */
public static char getRandomCharacters(char ch1, char ch2) {
return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
}

/** Generate random lowercase letters (this is the method that I used) */
public static char getLowercase() {
return getRandomCharacters('a', 'z');
}

/** Generate random uppercase letters */
public static char getUppercase() {
return getRandomCharacters('A', 'Z');
}
}

我非常感谢您提供的任何帮助。谢谢。

更新:问题已解决。感谢穆罕默德·侯赛因·塔勒布。为你+1。

最佳答案

尝试这样的事情:

public Map<Character, Integer> count( final char[] text )
{
final Map<Character, Integer> result = new TreeMap<>();
for ( final char c : text )
{
Integer i = result.get( c );
if ( null == i )
i = 0;
result.put( c, i + 1 );
}
return result;
}

至于你的问题:

//counting every character loop
int[] count = countLetter(kar);

方法 countLetter() 返回单个 int,并且无法转换为 int[]...

关于java - 如何在Java中对随机字母数组求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44600798/

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