用Java编写随机语句生成器

时间:2015-02-11 11:36:38

标签: java

我正在尝试创建一个用英语生成随机句子的程序(根据一般的英语语法规则)。

我不断收到子程序randomSentence()的错误。我错过了什么?关于如何简化一切的任何建议?

我收到的错误是:

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) {}
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这是最明显的错误:

public static void randomSentence() {

    System.out.print(randomSentence());

}

以递归方式调用该方法。

此外,该程序充满了概念错误,您需要更好地研究OO编程范例。

答案 1 :(得分:0)

确定。 第一个问题:

System.out.print(randomSentence());

randomSentence()返回void,因此基本上 <{1}}无法打印。

下一步 - &gt;即使你碰巧解决了这个问题。您以递归方式调用print() ,您将获得randomSentence()。请检查您的设计/代码。