黑马程序员技术交流社区
标题:
随机字符串统计字符出现次数(区分大小写不能使用集合)
[打印本页]
作者:
你是对的
时间:
2015-12-4 21:01
标题:
随机字符串统计字符出现次数(区分大小写不能使用集合)
public class SumOfChar {
public static void main(String[] args) {
for(char i='A';i<='Z';i++){
int a=checkNumber(RandomStr(),i);
if(a==0){
}else{
System.out.print(i+"("+a+")"+",");
}
}
for(char i='a';i<='z';i++){
int a=checkNumber(RandomStr(),i);
if(a==0){
}else{
System.out.print(i+"("+a+")"+",");
}
}
}
public static int checkNumber(String str,char a){
int count=0;
char[] arr=str.toCharArray();
for (int i = 0; i < arr.length; i++) {
if(arr[i]==a){
count++;
}
}
return count;
}
public static String RandomStr() {
String str="";
char[] ch=new char[100];
for(int i=0;i<20;i++){
if((int)(Math.random()*2+1)==1){
ch[i]=(char)(Math.random()*26+65);
}else{
ch[i]=(char)(Math.random()*26+97);
}
str+=ch[i];
}
return str;
}
}
复制代码
作者:
DayBreak
时间:
2015-12-4 22:04
可以给我技术分么{:3_47:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2