Perl基本变量及语法练习

1. 半径为12.5时,圆的周长应该是多少?

#!/usr/bin/perl
$radius = 12.5;
print $radius * 2 * 3.141592654."\n";

2. 修改上题,提示用户键入半径,求出圆的周长。

#!/usr/bin/perl
print "Please input the radius of the circle: ";
$radius = <STDIN>;
print "The perimeter of the circle is: ";
print $radius * 2 * 3.141592654."\n";

3. 修改上题,当用户输入小于0的半径时,输出0而不是负数。

#!/usr/bin/perl
print "Please input the radius of the circle: ";
$radius = <STDIN>;
print "The perimeter of the circle is: ";
if ($radius lt 0) {
    print "0"."\n";
}else {
    print $radius * 2 * 3.141592654."\n";
}

4. 提示用户输入两个数字,求两者的乘积?

#!/usr/bin/perl
print "Please input the first number: ";
$num_1 = <STDIN>;
print "Please input the second number: ";
$num_2 = <STDIN>;
$result = $num_1 * $num_2;
print "The product of the two numbers is: $result\n";

5. 用户键入一个字符串和一个数字,以数字为重复次数,连续输出字符串。

#!/usr/bin/perl -W
print "Please input a string: ";
$string = <STDIN>;
print "Please input a positive number: ";
$num = <STDIN>;
$result = $string x $num;
print "The result is:\n$result";
    原文作者:SophieSg
    原文地址: https://www.jianshu.com/p/9a6e481f6b10
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞