首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

[D]perl 小程序求批改(5)

2012-09-06 
[D]perl 小程序求修改(5)Perl codeuse 5.010print please input a number\n$favorate13chomp($num

[D]perl 小程序求修改(5)

Perl code
use 5.010;print "please input a number\n";$favorate=13;chomp($num=<STDIN>);given($num){  when (!/^\d+$/) {print "is not a number\n"}  @div=&divisors($_);  @empty;  when (@div~~@empty){print"number is prime and is odd\n"}  when (@div~~2){print "this is an even\n";continue}  when (!(@div~~2)) {print "this is an odd\n";continue};  when (@div~~$favorate){print "This number can be divided by $favorate\n";continue}  when (!(@div~~$favorate)){print "this number can not be divided by $favorate\n"}  default {print"$_ is divisible by @divisors"}           }sub divisors {     my $number=shift;     my @divisors=();     foreach my $divisor (2..($number/2)) {     push @divisors, $divisor unless $_ % $divisor;     }return @divisors;}
报告输入数字的奇偶情况,是否是质数,是否可以被13整除。这个程序总是输出奇数,不能被13整除而不管真实情况是什么。问题出在哪呢?

--------------------------
Double行动:
原帖分数:20
帖子加分:20


[解决办法]
Perl code
push @divisors, $divisor unless $num % $divisor; 

热点排行