我正在尝试制作一个程序,为用户生成随机的帐户名。用户将点击按钮,并将帐户名复制到剪贴板中。GUI部分已经工作正常,但我无法想出处理字符串随机生成的最佳方法。
用户名中允许使用的字符:A-Z a-z _
不能包含数字、其他符号和连续两个相同的字符。
必须是六位长度。
我的想法:
create an array of characters:
[ _, a, b, c, d ... etc ]
Generate a random integer between 0 and array.length - 1
and pick the letter in that slot.
Check the last character to be added into the output String,
and if it's the same as the one we just picked, pick again.
Otherwise, add it to the end of our String.
Stop if the String length is of length six.
有没有更好的方法?也许可以使用正则表达式?我觉得我现在想做的方式可能很糟糕。
a_a_a_
可以吗? - user1803551