Java中难度较高的题①

使用StringBuffer实现对字符的筛选

/**
         * 1:自定义方法:实现获得一个随机字符的功能(要求,随机字符必须是26个英文字符,大小写也随机)。
         * 2:使用上面定义的方法,去获得20个随机字符,添加到一个StringBuffer对象中。并打印内容。然后实现将对象中的小写字符删除,并打印剩余的大写字符。
         */


        StringBuffer sb=new StringBuffer();

        for (int i=0;i<=19;i++){
            sb.insert(i,GetWordRandom.getRandomChar());
        }
        System.out.println(sb);


        for(int j=0;j<sb.length();j++){//长度要减一
            char c=sb.charAt(j);
            if (c>='a'&&c<='z'){
                sb.deleteCharAt(j);
                j--;
            }
        }
        System.out.println(sb);



//        Random random = new Random();
//        //随机的int值
//        int i = random.nextInt();
//        System.out.println(i);

    }
}
class GetWordRandom {

    public static char getRandomChar(){//使用转换的方式求出随机的英文字母,大写为65-90    小写的97-122
        final int min='A';
        final int max = 'Z'+1;
        //随机的大写字符
        char ran = (char)getNewRandom(min,max);
       ran =  getNewRandom(0,2) == 0 ?  ran : (char)(ran + 32);
       return ran;
    }

    public static int getNewRandom(int min, int max) {
        return (int) (Math.random() * (max - min) + min);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值