2015-02-11 21 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。請檢查您的設計/代碼。