生成随机数,相信许多人都会,但是如何生成随机字母呢?
其实很简单,字符都是对应的A码,只要先生成字母的A码,接着转化成字符就成功了。
package test;
public class randChar {
public static void main(String[] args){
//初始化\
int i,j,k;
i = j = 0;
//主体
while(i < 50){
random();
j++;
i++;
if(j == 5){
System.out.println();
j = 0;
}
}
}
//生成随机字母
public static void random(){
char ch = (char)(Math.random()*26 + 'a');
System.out.print(ch + " ");
}
}