Perl:标量数据-练习3
时间:2010-04-02 来源:xuezhilei40308
练习3:修改练习2,当用户输入小于0的数字时,程序输出周长为0,而非负数。
sourcecode:
#!/usr/bin/perl -w
print "please input the value of radius:\n";
chomp ($j=<STDIN>);
$i="3.1415926";
if ($j<=0){
$circu=0;
}else{
$circu=2*$i*$j;
}
print "The circumference of $j is $circu";
note:
1. if控制结构
a. if(...){...}else{...}比较两个值,并根据比较结果做出判断。本练习中比较用户输入的值是否大于0.
b. 将块中的代码做个缩进,便于阅读,本练习中使用TAB键。
c. if()括号中为条件判断语句。该语句返回true或者false,perl判断返回值的规则是:如果值是数字,0为假,其余为真;如果值为字符串,‘’空串为假,其余为真;
2. 比较运算符
a.数字的比较: == != < > <= >=
b.字符串的比较:eq ne lt gt le ge
c.注意‘35’eq '35.0' 返回值为假,因为是按照字符串来比较的。
3. 根据答案修改print的内容为:
print "The circumference of a circle of radius $j is $circu\n";
sourcecode:
#!/usr/bin/perl -w
print "please input the value of radius:\n";
chomp ($j=<STDIN>);
$i="3.1415926";
if ($j<=0){
$circu=0;
}else{
$circu=2*$i*$j;
}
print "The circumference of $j is $circu";
note:
1. if控制结构
a. if(...){...}else{...}比较两个值,并根据比较结果做出判断。本练习中比较用户输入的值是否大于0.
b. 将块中的代码做个缩进,便于阅读,本练习中使用TAB键。
c. if()括号中为条件判断语句。该语句返回true或者false,perl判断返回值的规则是:如果值是数字,0为假,其余为真;如果值为字符串,‘’空串为假,其余为真;
2. 比较运算符
a.数字的比较: == != < > <= >=
b.字符串的比较:eq ne lt gt le ge
c.注意‘35’eq '35.0' 返回值为假,因为是按照字符串来比较的。
3. 根据答案修改print的内容为:
print "The circumference of a circle of radius $j is $circu\n";
相关阅读 更多 +
排行榜 更多 +