代码保存体
public class Test {
// 创建一个长度是100的字符串数组
// 使用长度是2的随机字符填充该字符串数组
// 统计这个字符串数组里重复的字符串有多少种
public static void count(int Stringlength,int charlength){
String[] str = new String[Stringlength];
for (int i = 0;i < str.length;i++){
str[i] = GetString(charlength);
System.out.print(str[i] + " ");
if (i % 20 == 19){
System.out.println();
}
}
String a = "";
int n = 0;
for (int i = 0;i < str.length;i++){
int m = 0;
for (int j = i+1;j < str.length;j++){
if (str[i].equals(str[j])){
m++;
}
}
if (m > 0){
a += str[i] + " ";
n++;
}
}
if (n > 0){
System.out.print("重复的字符有" + n + "个\n" );
System.out.print("分别是" + a);
}else{
System.out.println("没有重复的字符串");
}
}
private static char GetChar(){
int a = (int)(Math.random()*2);
switch (a){
case 0:
return (char) (Math.random()*('9'-'0'+1)+'0');
case 1:
return (char) (Math.random()*('Z'-'A'+1)+'A');
}
return '淦';
}
public static String GetString(int num){
String str = "";
for (int i = 0;i < num;i++){
char a = GetChar();
str += a;
}
return str;
}
public static void main(String[] args) {
count(100,2);
}
}