看到别人好的代码,我保存保存

代码保存体

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值