zoukankan      html  css  js  c++  java
  • Day_09【常用API】扩展案例5_获取长度为5的随机字符串,字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成

    分析以下需求,并用代码实现

    •   1.定义String getStr(char[] chs)方法
           功能描述:获取长度为5的随机字符串,字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成
        2.定义main方法,方法内完成:
        	(1)定义长度为26,元素值为26个大写英文字母的数组chs
        	(2)传递数组chs调用getStr(char[] chs)方法,获取返回值,并在控制台打印返回值
      
    package com.itheima2;
    
    import java.util.Random;
    
    public class Test5 {
    	public static void main(String[] args) {
    		//定义长度为26,元素值为26个大写英文字母的数组chs
    		char[] chs = new char[26];
    		int index = 0;
    		for(int x = 'A';x <= 'Z';x++) {
    			chs[index++] = (char)x;                          //这里没搞明白,是做了一个强转?
    		}
    		String str = getStr(chs);
    		
    		System.out.println("result:"+str);
    	}
    	
    	/*
    	 * 获取长度为5的随机字符串,字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成
    	 * 获取1个0-9之间(包含0和9)的整数组成
    	 * 
    	 * 返回值类型:String
    	 * 参数列表:char[] chs
     	 */
    
    	public static String getStr(char[] chs) {
    		String str = "";
    		Random rand = new Random();
    		for(int x = 0;x < 4;x++) {
    			str = str + chs[rand.nextInt(26)];				//这是char值转换成了字符串?
    		}
    		str += rand.nextInt(10);
    		return str;
    	}
    }
    
    

    控制台输出内容
    在这里插入图片描述

  • 相关阅读:
    cache buffers chains ,buffer busy waits
    关于I/O的一些脚本
    模拟buffer busy waits等待事件
    找出热点块所属的用户,对象名,类型
    找到引起磁盘排序的SQL
    db file parallel write,write complete waits
    free buffer waits
    检查日志文件是否传输到备用数据库
    模拟direct path read 等待事件
    3系统启动后的 wifi 加载过程
  • 原文地址:https://www.cnblogs.com/zzzsw0412/p/12772537.html
Copyright © 2011-2022 走看看