源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java生成字母数字组合的随机数示例 java生成随机数

  • 时间:2022-07-06 14:50 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java生成字母数字组合的随机数示例 java生成随机数
[u]复制代码[/u] 代码如下:
package com.test; import java.util.Random; public class GenerateRandomNumber {  public static void main(String[] args) {   System.out.println("生成的10为随机数为:" + getCharAndNumr(10));  }  /**   * java生成随机数字和字母组合   * @param length[生成随机数的长度]   * @return   */  public static String getCharAndNumr(int length) {   String val = "";   Random random = new Random();   for (int i = 0; i < length; i++) {    // 输出字母还是数字    String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";    // 字符串    if ("char".equalsIgnoreCase(charOrNum)) {     // 取得大写字母还是小写字母     int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;     val += (char) (choice + random.nextInt(26));    } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字     val += String.valueOf(random.nextInt(10));    }   }   return val;  } }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部