在 Java 中从任意两个字符中生成一个随机字符
如何在两个特定字符之间生成随机字符?例如;我想生成h"或v"之一.
How do I generate a random character between two specific characters? For e.g; I want to generate either one of 'h' or 'v'.
谢谢
推荐答案
如果要生成一个字符,如你所说,h或v,你可以使用Random 类如图 这里.比如随机数大于0.5,则选v,否则选h.
If you want to generate a character, as you say, either h or v, you can generate a random number using the Random class as shown here. If for instance the random number is greater than 0.5, then choose v, if otherwise, choose h.
另一方面,如果你有一个字母范围,你可以生成一个包含你想要的字符的数组并生成一个随机数,该随机数将用作选择随机字母的索引,或者,你可以生成 65(A 的 Aschii)和 90(Z 的 Aschii)之间的随机数.您可以在这里
On the other hand, if you have a range of letters, you can either generate an array with the characters you want and generate a random number which will be used as an index to choose the random letter, or else, you can generate random number between 65 (Aschii for A) and 90 (Aschii for Z). You can find more Aschii characters here
相关文章