用26个字母生成随机单词,乱文

本文介绍如何利用Java编程,结合ASCII码和随机数生成器,从26个英文字母中随机组合出单词,实现简单的乱文生成。通过这种方式,可以创建独特的随机字符串,有助于理解基础的字符串操作和随机数处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package cha;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
    public class aaaa {
        public static void main(String[] args) {
            Random rand = new Random();
            try (FileOutputStream fos = new FileOutputStream("张.txt")){
                for (int i = 0; i < 5000; i++) {
                    fos.write(randStr(rand.nextInt(3,15)).concat("\t\n").getBytes());
                }
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        public static String randStr(int n) {
            Random rand = new Random();
            String letter = "abcdefghijklmnopqrstuvwsyz";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++) {
                StringBuilder sb1 = new StringBuilder();
                int len = rand.nextInt(2,10)
                for (int j = 0; j < len; j++) {
                    sb1.append(letter.charAt(rand.nextInt(letter.length())));
                }
                sb1.append(" ");//

                String ts = new String(sb1);
                if (i ==0 ){
                    ts = ts.substring(0,1).toUpperCase().concat(ts.substring(1));
                }else if (i == n-1){
                    ts = ts.substring(0,ts.length()-1)+"!.?".charAt(rand.nextInt(3));
                }
                sb.append(ts);
            }
            return sb.toString()    ;
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值