2015-02-11 68 views
-1

我想创建一个程序,用英语生成随机语句(根据一般英语语法规则)。我的子程序randomSentence()不断收到错误。我错过了什么?还有关于如何简化一切的建议?用Java写一个随机语句生成器

我收到的错误是:

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

你是什么意思的错误?这是一个编译器错误?一个例外?... – manouti 2015-02-11 11:37:17

+1

如果我没有错,你会得到一个'StackOverflowError'你递归地调用'randomSentence()'。 – TheLostMind 2015-02-11 11:39:37

+0

如果randomSentence返回了一些东西,情况就是这样。因为它不应该编译... – Deltharis 2015-02-11 11:40:52

回答

0

这是最明显的错误:

public static void randomSentence() { 

    System.out.print(randomSentence()); 

} 

您递归调用的方法。此外,程序中充满了概念错误,您需要更好地研究OO编程范例。

0

好的。该第一问题:

System.out.print(randomSentence());

randomSentence()回报void,所以基本上没有什么print()打印。

Next - >即使你碰巧解决了这个问题。您正在递归调用randomSentence(),您将得到StackOverflowError。请检查您的设计/代码。