单独的 "for" 循环迭代

Separate "for" loop iterations

我将如何使我的四个循环的每次迭代都是随机的。截至目前的结果使所有 20 个句子都输出相同的输出,尽管它每次都从在静态 class OuterWord 中创建的四个数组中选择随机单词。我省略了代码中我认为不需要的部分,包括为每个数组和句子创建的 getMethods,希望这不会对我的问题产生负面影响。

`public class SentenceMaker {

static class OuterWord {
    //arrays
    String [] article = {"the", "a", "one", "some", "any"}; //array for articles
    String [] noun = {"boy", "girl", "dog", "town", "car"}; //array for nouns
    String [] verb = {"drove", "jumped", "ran", "walked", "skipped"}; //array for verbs
    String [] preposition = {"to", "from", "over", "under", "on"}; //array for prepositions

}
static class OuterSentence {
   int random = (int) (Math.random()*5);
    OuterWord word = new OuterWord();

    //sentence structure article-noun-verb-preposition-article-noun
    //StringBuilder with append, attempts to concatenate with .append
    //This code works as well
    StringBuilder sentence = new StringBuilder()
                    .append(word.article[random].substring(0, 1).toUpperCase())
                    .append(word.article[random].substring(1))
                    .append(" ")
                    .append(word.noun[random])
                    .append(" ")
                    .append(word.verb[random])
                    .append(" ")
                    .append(word.preposition[random])
                    .append(" ")
                    .append(word.article[random])
                    .append(" ")
                    .append(word.noun[random])
                    .append(".");
}


public static void main(String args[]) {
    //random element from arrays
    //int random = (int) (Math.random()*5);

    //instantiation of the Word and Sentence classes
    OuterWord word = new OuterWord();
    OuterSentence s = new OuterSentence();


    //loop initializer
    int counter = 0;

    //for loop for 20 iterations
    for(int i = 0; i < 20; i++){
        //counter to keep track of iterations
        counter = counter + 1;
        //sentence article-noun-verb-preposition-article-noun
        System.out.println("Sentence " + counter + ": " + s.sentence.);
    }
}

}`

只需将 OuterSentence 对象移动到 for 循环。

for(int i = 0; i < 20; i++){
        OuterSentence s = new OuterSentence();
        //counter to keep track of iterations
        counter = counter + 1;
        //sentence article-noun-verb-preposition-article-noun
        System.out.println("Sentence " + counter + ": " + s.sentence);
    }

上面的方法可读性很强,或者另一种方法是在 System.out.println 语句中创建新对象,如下所示:

System.out.println("Sentence " + counter + ": " + new OuterSentence().sentence);

在这两种方式中,我们都为每个语句创建一个新对象,以便生成新的随机数,从而形成新的句子。

我会 start 通过将所有内容移动到一个 class 中,你的代码已经不必要地复杂化了组织起来了。

private static String[] article = { "the", "a", "one", "some", "any" };
private static String[] noun = { "boy", "girl", "dog", "town", "car" };
private static String[] verb = { "drove", "jumped", "ran", "walked", "skipped" };
private static String[] preposition = { "to", "from", "over", "under", "on" };

接下来,我将编写一个方法来获取给定单词的首字母大写。您只需要将第一个字符大写,但 StringBuilder 非常适合。

private static String getTitleCase(String w) {
    StringBuilder sb = new StringBuilder(w);
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    return sb.toString();
}

然后构建一个句子,我会将其移动到一个方法中。您目前只能获得一个随机索引并构建一个句子。相反,获取适合每个数组长度的随机值,我会使用 StringJoiner 来简化添加单词之间的空格。喜欢,

private static String buildSentence() {
    Random rand = ThreadLocalRandom.current();
    StringJoiner sj = new StringJoiner(" ");
    sj.add(getTitleCase(article[rand.nextInt(article.length)]));
    sj.add(noun[rand.nextInt(noun.length)]);
    sj.add(verb[rand.nextInt(verb.length)]);
    sj.add(preposition[rand.nextInt(preposition.length)]);
    sj.add(article[rand.nextInt(article.length)]);
    sj.add(noun[rand.nextInt(noun.length)]);
    return String.format("%s.", sj);
}

最后调用上面的方法二十次

public static void main(String[] args) {
    for (int i = 0; i < 20; i++) {
        System.out.println(buildSentence());
    }
}