如果一篇文章写得前言不搭后语,逻辑不通,我们称之为“狗屁文章”。
或许,你应该怀疑一下这样文章是不是由机器写的,比如,下面20句话就是由Java随机生成的。
运行结果:
一些城镇驾驶来这个小汽车。
一些小狗跑过有一个城镇。
这个小汽车越上这个小汽车。
有一个小狗跑上有一个小狗。
有一个小汽车越下一些城镇。
这个小汽车越来任何小狗。
一些小狗越过一些小汽车。
这个小狗冲过任何男孩。
这个男孩跑上一个女孩。
这个男孩跑上这个小汽车。
任何女孩跑过有一个城镇。
任何小汽车跑来这个男孩。
一个小汽车跑来有一个小汽车。
一些小狗跑下一个女孩。
一个城镇驾驶向任何男孩。
任何女孩越下任何小狗。
一个城镇跑向有一个城镇。
这个小狗跑过一个小狗。
一些城镇走上一个小汽车。
这个小汽车跑下一个女孩。
代码如下:
import java.math.*; /**Java how to program, 10th edition 14.5 (Random Sentences) Write an application that uses random-number generation to create sentences. Use four arrays of strings called article, noun, verb and preposition. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The application should generate and display 20 sentences. The article array should contain the articles "the", "a", "one", "some" and "any"; the noun array should contain the nouns "boy", "girl", "dog", "town" and "car"; the verb array should contain the verbs "drove", "jumped", "ran", "walked" and "skipped"; the preposition array should contain the prepositions "to", "from", "over", "under" and "on". * @author pandenghuang@163.com*/ public class RandomSentencesCN { public static String randomSentence(){ String[] nouns={"男孩", "女孩", "小狗", "城镇" ,"小汽车"}; String[] articles={"这个", "有一个", "一个", "一些" , "任何"}; String[] verbs={ "驾驶", "冲", "跑", "走" ,"越"}; String[] prepositions={ "向", "来", "过", "下" ,"上"}; int rNoun1st=(int)(Math.random()*5); int rArticle1st=(int)(Math.random()*5); int rVerb=(int)(Math.random()*5); int rPrepostion=(int)(Math.random()*5); int rNoun2nd=(int)(Math.random()*5); int rArticle2nd=(int)(Math.random()*5); String randomSentence=articles[rArticle1st]+nouns[rNoun1st]+ verbs[rVerb]+prepositions[rPrepostion]+ articles[rArticle2nd]+nouns[rNoun2nd]+"。"; return randomSentence; } public static void main(String[] args) { for (int i=0;i<20;i++){ System.out.println(randomSentence()); } } }