Is there functionality to generate a random character in Java?

前端 未结 18 2105
难免孤独
难免孤独 2020-11-28 06:00

Does Java have any functionality to generate random characters or strings? Or must one simply pick a random integer and convert that integer\'s ascii code to a character?

18条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 06:04

    String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    char letter = abc.charAt(rd.nextInt(abc.length()));
    

    This one works as well.

提交回复
热议问题