新手笔记之方法调用的生成随机字符

方法这章很难!

生成随机字符

1、生成从0到65 535之间的一个随机整数(注意:因为0 <= Math.random() <1.0,必须给65 535上加1):
(int) (Math.random() * (65 535 + 1))
2、生成一个随机小写字母
小写字母的统一码是一串连续的整数,从小写字母a的统一码开始,然后是b、c……z的统一码。a的统一码是:(in) ‘a'
所以,(int)‘a’到(int)‘z’之间的随机整数是:
(int) ((int)'a' + Math.random() * ((int) -(int)'a' + 1)
//生成随机字符
import java.util.Scanner;

public class Study4{
	
	public static char getStudy4(char ch1,char ch2){
		return(char)(ch1 + Math.random() * (ch2 - ch1 + 1));
	}
	public static char getRandomLowerCaseLetter(){
		return getStudy4('a','z');
	}
	public static char getRandomUpperCaseLetter(){
		return getStudy4('A','Z');
	}
	public static char getRandomDigitCharacter(){
		return getStudy4('0','9');
	}
	public static char getStudy4(){
		return getStudy4('\u0000','\uFFFF');
	}
	
	//显示175个随机的大写写字母
public static void main(String[] args) {
	final int N = 175;
	final int C = 25;
	for (int i = 0;i < N;i++){
		char ch = Study4.getRandomUpperCaseLetter();//定义和调用这类方法时仍然要使用括号
		if((i + 1) % C == 0)
			System.out.println(ch);
		else
			System.out.print(ch);
	
	}}}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值