用Java编写随机语句生成器

jw5wzhpr  于 2023-02-20  发布在  Java
关注(0)|答案(2)|浏览(197)

我正在尝试创建一个程序,生成随机的英语句子(根据一般英语语法规则)。
我的子程序randomSentence()一直有bug。我错过了什么?还有关于如何简化一切的建议吗?
我收到的错误是:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method print(boolean) in the type PrintStream is not applicable for the arguments (void)
at SentenceGenerator.randomSentence(SentenceGenerator.java:80)
at SentenceGenerator.main(SentenceGenerator.java:86)

代码:

import java.util.ArrayList;
import java.util.Random;

public class SentenceGenerator {

    private StringData[] stringData;
    // An array containing all of the possible words. This is a nested class.

    /**
    * An object of this type holds the word that will be randomly chosen and printed
    */
    public class StringData {
        String[] conjunction = {"and", "or", "but", "because"};
        String[] proper_noun = {"red", "Jane", "Richard Nixon", "Miss America"};
        String[] common_noun = {"man", "woman", "fish", "elephant", "unicorn"};
        String[] determiner = {"a", "the", "every", "some"};
        String[] adjective = {"big", "tiny", "pretty", "bald"};
        String[] intransitive_verb = {"runs", "jumps", "talks", "sleeps"};
        String[] transitive_verb = {"loves", "hates", "sees", "knows", "looks for", "finds"};

        //find out how many words there are in each list
        int conjunctionLength = conjunction.length;
        int proper_nounLength = proper_noun.length;
        int common_nounLength = common_noun.length;
        int determinerLength = determiner.length;
        int adjectiveLength = adjective.length;
        int intransitive_verbLength = intransitive_verb.length;
        int transitive_verbLength = transitive_verb.length;

        //Generate random numbers
        int rand1 = (int) (Math.random()*conjunctionLength);
        int rand2 = (int) (Math.random()*proper_nounLength);
        int rand3 = (int) (Math.random()*common_nounLength);
        int rand4 = (int) (Math.random()*determinerLength);
        int rand5 = (int) (Math.random()*adjectiveLength);
        int rand6 = (int) (Math.random()*intransitive_verbLength);
        int rand7 = (int) (Math.random()*transitive_verbLength);

        String word1 = conjunction[rand1];
        String word2 = proper_noun[rand2];
        String word3 = common_noun[rand3];
        String word4 = determiner[rand4];
        String word5 = adjective[rand5];
        String word6 = intransitive_verb[rand6];
        String word7 = transitive_verb[rand7];
    }

    static void Sentence() {
        String sentence() = SimpleSentence();
    }

    /**
     * subroutine that defines how SimpleSentence is put together, nesting NounPhrase and VerbPhrase subroutines
     */

    public static String SimpleSentence() {
        String simple_sentence = NounPhrase() + VerbPhrase();
        return "";
    }

    /**
     * subroutine that defines the nested variable NounPhrase
     */
    public static String NounPhrase() {
        String noun_phrase = proper_noun[word2], determiner[word4], common_noun[word3]; 
        return "";
    }

    /**
     * subroutine that defines the nested variable VerbPhrase
     */
    static void VerbPhrase() {
        String verb_phrase = intransitive_verb[word6], transitive_verb[word7], NounPhrase();
    }

    /**
     * Final subroutine that prints out the random sentence
     */
    public static void randomSentence() {
        System.out.print(randomSentence());
    }

    public static void main(String[] args) {
        while (true) {
            randomSentence();
            System.out.println(".\n\n");
            try {
                Thread.sleep(3000);
            }
            catch (InterruptedException e) {}
        }
    }
}
3pvhb19x

3pvhb19x1#

这是最明显的错误:

public static void randomSentence() {

    System.out.print(randomSentence());

}

递归调用该方法。
此外,程序充满了概念性错误,您需要更好地学习OO编程范式。

ws51t4hk

ws51t4hk2#

好的。第
System.out.print(randomSentence());
randomSentence()返回void,所以基本上print()没有什么要打印的。

Next--〉即使您碰巧修复了此问题。您正在 * 递归 * 调用randomSentence(),您将得到一个StackOverflowError。请检查您的设计/代码。

相关问题