a-z
첫 번째 소문자 알파벳 문자의 ASCII 코드는 65( a) 마지막은 97+26=122(z)입니다. 생성된 숫자는 [97, 122] 범위 또는 [0,26] + 97.Random rand = new Random();ASCII 코드 97을 'a'로 바꿀 수 있습니다:
char c = (char)(rand.nextInt(26) + 97);
System.out.println(c);
(char)(rand.nextInt(26) + 'a');
문자열 생성
n 문자인 경우 앞의 코드는 for.
Random rand = new Random();
문자열 str="";
for(int i = 0 ; i < 20 ; i++){
char c = (char)(rand.nextInt(26) + 97);
str += c;
System.out.print(c+" ");
}
f s e u k t m d a e b i m u y a y u s n
집합에서 영숫자 생성
이 예에서 정의된 문자 집합에서 영숫자 문자를 생성합니다. 이렇게 해봅시다.
- 원하는 세트로 문자열을 만듭니다
- 문자열 길이 검색
- Call rand.nextInt 위치를 반환합니다. k 0과 길이 사이-1
- alphabet.charAt(k)은 알파벳의 임의성입니다
Random rand = new Random();
문자열 알파벳="abcd1235";
int 길이 = alphabet.lentgh();
for(int i = 0; i < 30; i++) {
int k = rand.nextInt(길이);
System.out.print(alphabet.charAt(k)+" ");
}
b 1 3 1 d b b b 2 5 5 5 b a c 5 1 2 b 5 2 c b a 3 3 5 3 d b c 1
Commentaires (0)
Laisser un commentaire
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Chargement des commentaires...