版权声明:此文章为作者筱睿_原创文章,转载请附上博客链接 https://blog.csdn.net/qq_43756486/article/details/89502880
题例要求
编写一个采用随机函数生成句子的游戏。现有四个字符串数组:article,noun,verb,preposition,他们的内容分别是:
the、a、one、some、any;
boy、girl、dog、town、car;
drove、jumped、ran、walked、skipped;
to、from、over、under、on;
依据句法要求:article+noun+verb+preposition+article+noun,编写程序以产生二十个句子
java实现
public class sentence {
public static void main(String args[])
{
//用字符串数组存放四组单词
String article[]={"the","a","one","some","any"},
noun[]= {"boy","girl","dog","town","car"},
verb[]= {"drove","jumped","ran","walked","skipped"},
preposition[]= {"to","from","over","under","on"};
for(int jj=0;jj<20;jj++) //循环二十次,输出二十个句子
{ //用Math.random函数产生[0,1)的随机数,乘5取整作为数组随机下标
System.out.println("第"+(jj+1)+"个句子: "
+article[(int)(Math.random()*5)]+" "
+noun[(int)(Math.random()*5)]+" "
+verb[(int)(Math.random()*5)]+" "
+preposition[(int)(Math.random()*5)]+" "
+article[(int)(Math.random()*5)]+" "
+noun[(int)(Math.random()*5)]);
}
}
}