java

gyxevil 2018-03-02 04:28:46
java实现输出用户姓名为1到40位的随机字符串,由a-zA-Z字母组成,且第一位大写,例如Lucy、Fei、A、Pjskdfiesdfjksd;
...全文
352 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_39670158 2018-03-02
  • 打赏
  • 举报
回复
public static void main(String[] args) {
		char c = (char) ( 'A'+new java.util.Random().nextInt(888)%24); //首字母大写    %24防止超过Z
		StringBuilder sb = new StringBuilder();
		sb.append(c);		//把首字母放进去
		for(int i=0;i<=new java.util.Random().nextInt(888)%39;i++){	   //后面字符串长 0-39
			sb.append(getName());		//将获取到的随机字母放进sb
		}
		System.out.println(sb);
	}
	//该方法返回一个随机字母
	public static char getName(){	//获取 0-47 代表  a-zA-Z
		int letter = new java.util.Random().nextInt(888)%48;
		return letter>=24? (char)('A'+letter-24):(char)('a'+letter);
	}
oyljerry 2018-03-02
  • 打赏
  • 举报
回复
[A-Z][a-zA-Z]{0,39}
xiongdejun 2018-03-02
  • 打赏
  • 举报
回复
public static void main(String[] args) { // 随机生成几位字符串 int number = (int) (1 + Math.random() * (40 - 1 + 1)); String str[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; String str1[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; StringBuffer sb = new StringBuffer(); // 首字母大写 sb.append(str1[(int) ( Math.random() * (26 - 1 + 1))]); for (int i = 0; i < number - 1; i++) { sb.append(str[(int) ( Math.random() * (26 - 1 + 1))]); } System.out.println("随机1-40位字母组成的名字: " + sb.toString()); }

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧