国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

perl寫的一個隨機編故事的程序(rand隨機函數)

2020-06-04 20:30:17
字體:
來源:轉載
供稿:網友
代碼如下:
#!/bin/perl
use strict; 
use warnings; 

#定義變量 
my $count; 
my $input; 
my $number; 
my $sentence; 
my $story; 

#定義四個數組 
#定義了人物數組 
my @nouns= 

   'Dad', 
   'TV', 
   'Mom', 
   'Groucho', 
   'Rebecca', 
   'Harpo', 
   'Robin Hood', 
   'Joe and Moe', 
); 

#定義了動作數組 
my @verbs= 

   'ran to', 
   'giggled with', 
   'put hot sauce into the orange juice of', 
   'exploded', 
   'dissolved', 
   'dissolved', 
   'sang stupid songs with', 
   'jumped with', 
 ); 

 #定義了地點數組 
 my @prepositions= 
 ( 
     'at the store', 
     'over the rainbow', 
     'at the beach', 
     'before dinner', 
     'in New York City', 
     'in a dream', 
     'around the world', 
 ); 

 do  
 { 
    #每次運行以前,清空$story的內容 
    $story=''; 
    #以下是隨機組合生成故事 
    for ($count =0;$count<6;$count++) 
    { 
       #我們劃分一下結構,scalar @nouns 是獲取@nouns數組元素的個數,然后用rand總這幾個數中隨機生成幾個,最后int是將得到的數取整 
       $sentence =$nouns[int(rand(scalar @nouns))] 
       ." " 
       .$verbs[int(rand(scalar @verbs))] 
       ." " 
       .$nouns[int(rand(scalar @nouns))] 
       ." " 
       .$prepositions[int(rand(scalar @prepositions))] 
       .'.'; 
       $story .=$sentence; 
   } 
   print "/n",$story,"/n"; 
   print "/nType /"quit/"to quit, or press Enter to continue:"; 
   $input=<STDIN>; 

#這里是利用正則表達式來匹配首字符是q的字符串,如果有,那么就退出,/^是匹配起始位置,/i是不區分大小寫 
until ($input=~/^/s*q/i); 
exit;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 增城市| 玛曲县| 松溪县| 绥德县| 保康县| 大英县| 双桥区| 济南市| 池州市| 渭南市| 大余县| 临武县| 景宁| 大连市| 佛山市| 广河县| 长白| 明光市| 平塘县| 温泉县| 达州市| 张家口市| 洪泽县| 克什克腾旗| 吉木萨尔县| 天津市| 鄯善县| 张家川| 东乡| 巴林左旗| 德惠市| 襄城县| 长治县| 苍南县| 卢湾区| 远安县| 崇明县| 鄂州市| 论坛| 屏山县| 淳化县|