Is there functionality to generate a random character in Java?

前端 未结 18 2102
难免孤独
难免孤独 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:29

    public static void  main(String[] args) {
    
      //  System.out.println("Enter a number to changeit at char  ");
        Random random = new Random();
    
        int x = random.nextInt(26)+65;    //0  to 25
        System.out.println((char)x);
    }
    

提交回复
热议问题