概述
public class F {
public static void main(String[] args)
{
char[] chars = createArray();
System.out.println("The lowercase letters are : ");
displayArray(chars);
int[] counts =countLetters(chars);
System.out.println();
System.out.println("The occurences of each letter are:");
displayCounts(counts);
}
private static void displayArray(char[] chars) {
for (int i = 0; i<chars.length; i++)
{
if((i+1)%20==0)
System.out.println(chars[i]);
else
System.out.print(chars[i] +" ");
}
}
private static char[] createArray() {
char[] chars =new char[100];
for (int i = 0; i<chars.length; i++)
chars[i] = (char)('a'+Math.random()*('z'-'a'+1));//随机获取a-z的字母
return chars;
}
private static int[] countLetters(char[] chars) {
int[] counts =new int[26];
for (int i = 0; i<chars.length; i++)
{
counts[chars[i]-'a']++;
}
return counts;
}
private static void displayCounts(int[] counts) {
for (int i = 0; i<counts.length; i++)
{
if ( (i+1)%10 == 0)
System.out.println(counts[i] + " " + (char)(i+'a'));
else
System.out.print(counts[i] + " " + (char)(i+'a') + " ");
}
}
}
最后
以上就是鳗鱼蜜蜂为你收集整理的随机打印100个小写字母,并打印出每个字母出现的次数的全部内容,希望文章能够帮你解决随机打印100个小写字母,并打印出每个字母出现的次数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复