我需要创建一个随机字符串,其长度应介于6到10之间,但它有时只生成3到5的长度。 这是我的代码。任何人都可以找到问题吗? :(
int lengthOfName = (int)(Math.random() * 4) + 6;
String name = "";
/* randomly choosing a name*/
for (int j = 0; j <= lengthOfName; j++) {
int freq = (int)(Math.random() * 100) + 1;
if(freq <= 6){
name += "a";
}if(freq == 7 && freq == 8){
name += "b";
}if(freq >= 9 && freq <= 11){
name += "c";
}if(freq >= 12 && freq <= 15){
name += "d";
}if(freq >= 16 && freq <= 25){
name += "e";
}if(freq == 26 && freq == 27){
name += "f";
}if(freq == 28 && freq == 29){
name += "g";
}if(freq >= 30 && freq <= 33){
name += "h";
}if(freq >= 34 && freq <= 48){
name += "i";
}if(freq == 49 && freq == 50){
name += "j";
}if(freq >= 51 && freq <= 55){
name += "k";
}if(freq >= 56 && freq <= 60){
name += "l";
}if(freq == 61 && freq == 62){
name += "m";
}if(freq >= 63 && freq <= 70){
name += "n";
}if(freq >= 71 && freq <= 75){
name += "o";
}if(freq == 76 && freq == 77){
name += "p";
}if(freq == 78){
name += "q";
}if(freq >= 79 && freq <= 84){
name += "r";
}if(freq == 85 && freq == 86){
name += "s";
}if(freq == 87 && freq == 88){
name += "t";
}if(freq >= 89 && freq <= 93){
name += "u";
}if(freq == 94){
name += "v";
}if(freq == 95 && freq == 96){
name += "w";
}if(freq == 97){
name += "x";
}if(freq == 98 && freq == 99){
name += "y";
}if(freq == 100){
name += "z";
}
}
答案 0 :(得分:20)
我很抱歉,但代码写得太差,无法挽救。我推荐这样的东西。
Random r = new Random(); // just create one and keep it around
String alphabet = "abcdefghijklmnopqrstuvwxyz";
final int N = 10;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
sb.append(alphabet.charAt(r.nextInt(alphabet.length())));
}
String randomName = sb.toString();
System.out.println(randomName);
要点是:
java.util.Random
,特别是nextInt(int n)
来获取给定范围内的随机int
StringBuilder
。charAt
为其字母编制索引。java.util.Random
int nextInt(int n)
StringBuilder
- 一个可变的字符序列。不幸的是,有很多。
String +=
会导致较长字符串的性能非常差for (int j = 0; j <= lengthOfName; j++)
是off-by-one-error freq == 7 && freq == 8
是一个逻辑矛盾我强烈建议您做一些小而简单的练习来学习Java基础知识。 codingbat.com很棒;它有数百个,它们会自动评分,因此您可以知道解决方案何时按预期工作。它有关于逻辑,字符串,数组等的部分。
最简单的解决方案是在字母表中加上重复:
String alphabet = "aab";
的概率为a
的两倍b
alphabet
答案 1 :(得分:5)
if(freq == X && freq == X+1)
等条件始终为false
。
您可能打算使用||
(OR)
答案 2 :(得分:5)
你的代码有很多重复同样的问题:
if(freq == 28 && freq == 29) { ... }
您告诉Java在freq equals to 28
和freq equals to 29
时遵循某个条件。不可能。您将需要使用OR运算符:
if(freq == 28 || freq == 29) { ... }
现在发生的事情是,当freq等于那些错误条件中的任何数字时,你的字符串中不会添加任何数字,它会变小。
答案 3 :(得分:3)
以下是我的解决方案:
import java.util.Random;
Random gen = new Random(474587); //put in random seed
int min = 6;
int max = 10;
// we want 20 random strings
for(int i=0; i < 20; i++){
int len = min+gen.nextInt(max-min+1);
StringBuilder s = new StringBuilder(len);
while(s.length() < len){
//97 is ASCII for character 'a', and 26 is number of alphabets
s.append((char)(97+gen.nextInt(26)));
}
System.out.println(s.toString());
}
输出样本:
zqwloh
jefcso
spcnhxyyk
tzlobaukn
keyxkn
cllhsxybz
ieaudei
bolfzqlxrl
scpfcbztyh
thkfrybffe
nbspabxjh
答案 4 :(得分:2)
if(freq == 49&amp;&amp; freq == 50){ name + =“j”;
实际上从来都不是真的。
答案 5 :(得分:1)
我打赌你不再需要答案,但由于我以前从未回答过堆栈溢出问题,我认为这是一个很好的热身问题。
其他人似乎遗漏的一件事是代码的频率方面。以下代码将根据您想要的频率创建10个长度为6到10的随机单词:
import java.util.Random;
public class Stuff {
public static void main(String[] args) {
Random rand = new Random();
int[] freqs = new int[] {6,8,11,15,25,27,29,33,48,50,55,60,62,70,75,77,78,84,86,88,93,94,96,97,99,100};
int numWords = 10;
for(int i = 0; i<numWords; i++)
{
String word = "";
int numLetters = 6 + rand.nextInt(5);
for(int j = 0; j<numLetters; j++)
{
int freq = rand.nextInt(100) + 1;
int index = 0;
while(freqs[index] < freq) index++;
word = word + (char)(97 + index );
}
System.out.println(word);
}
}
现在,我的问题是你能告诉我这是如何运作的吗?
JB
答案 6 :(得分:0)
仅供参考和完整,这是一个“简单”(但效率较低)的解决方案,假设字符串中存在数字不是一个大问题:
private static final Random random = new Random();
public static String generateRandomString() {
return new BigInteger((4 + random.nextInt(3)) * 8, random).toString(36);
}
这将生成一个长度为6~10(含)的[a-z0-9]
的随机字符串。
答案 7 :(得分:0)
我想出的是
import java.util.*;
public class RandomString6to10
{
public static void main(String[] args)
{
Random rnd = new Random();
Scanner scan = new Scanner (System.in);
String alphabets = "abcdefghijklmnopqrstuvwxyz";
int min1 = rnd.nextInt(5) + 6;
int min2 = rnd.nextInt(3) + 3;
System.out.println("How many strings do you want?");
int x = scan.nextInt();
for ( int i = 0; i < x; i++)
{
/* because you didn't tell us when should the generator decide to choose 6-10 or 3-5
so I made it random */
if (rnd.nextBoolean())
{
min1 = rnd.nextInt(5) + 6;
for (int t=0; t < min1; t++)
{
int randomString1 = rnd.nextInt(alphabets.length());
System.out.print(alphabets.charAt(randomString1));
}
System.out.println();
}
else
{
min2 = rnd.nextInt(3) + 3;
for (int j=0; j < min2; j++)
{
int randomString2 = rnd.nextInt(alphabets.length());
System.out.print(alphabets.charAt(randomString2));
}
System.out.println();
}
}
}
}