How to output random characters by their Unicode code
I'm trying to output Unicode characters in the range 9398, 10178.
import java.util.*;
public class Unicode {
public static void main(String[] args) {
Random rand_char = new Random();
int number = 10178;
int unicode = rand_char.nextInt(number) + 9398;
char smile = (char)('\u' + r.nextInt(unicode));
System.out.println(smile);
}
}
There are problems with '\u' in creating the variable.How can I solve this so that the program can output random characters in the range?
0
1 answers
No need for \u
- just cast the numeric character code to char
:
Random rand_char = new Random();
int number = 10178;
int unicode = rand_char.nextInt(number) + 9398;
char smile = (char)(unicode);
System.out.println(unicode);
System.out.println(smile);
12564
ㄔ
0
Author: insolor, 2020-02-17 05:47:16