gpt4 book ai didi

java - 有没有可能写一个像 python 一样短的随机字母生成器?

转载 作者:行者123 更新时间:2023-11-29 06:48:47 25 4
gpt4 key购买 nike

所以我想缩短我的代码,我问自己是否有可能 Java 中的随机字母生成器与 python 中的一样短。在 python 中,它只是一对一的衬里。

以下代码是我的代码:

int random = (int) Math.random()*25;

String[] letters ={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

String letter = letters[random];

最佳答案

如果“短”仅表示一行,那么以下任何一个都可以:

char letter = "abcdefghijklmnopqrstuvwxyz".charAt((int) (Math.random() * 26));

char letter = (char) ThreadLocalRandom.current().nextInt('a', 'z'+1);

char letter = (char) ('a' + Math.random() * 26);

关于java - 有没有可能写一个像 python 一样短的随机字母生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56510261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com