0
sub main{
my $mark;
my $grade;
my $calc;

@grade = ($mark>=0 and $mark<=39,$mark>=40 and $mark<=49,$mark>=50 and $mark<=59);
@calc(F+,D+,B+);

print "What is the student’s mark?"
chomp($mark = <STDIN>);

print "Your mark is 'mark' and grade is 'calc'"
}
main();

嗨,我是初学者,我想做的是制作不同的标记块,例如 @mark(0-39,40-49,50-59) 将分别指向 @calc(F+,D+,B+)。之后我可以打印出 $mark 以及与标记对应的等级。谢谢您的帮助。

4

2 回答 2

1

您可以使用一系列成绩。数组的每个条目都可以是一个哈希表,其中包含等级名称以及该等级的最小值和最大值:

my @grades = (
    { name => 'F+', min => 0, max => 39 },
    { name => 'D+', min => 40, max => 49 },
    { name => 'B+', min => 50, max => 59 }
    );

print "What is the student’s mark?\n";
chomp(my $mark = <STDIN>);

my $calc = "Unknown";
foreach my $grade (@grades) {
    if ($grade->{min} <= $mark && $mark <= $grade->{max}) {
        $calc = $grade->{name};
    }
}

print "Your mark is '$mark' and grade is '$calc'\n";
于 2013-04-28T07:08:13.487 回答
1

首先,始终使用use strict; use warnings;.

从最好的字母开始,找到范围开始小于标记的第一个字母。

my @letters      = qw( F+ D+ B+ );
my @letter_marks =   (  0,40,50);

sub get_letter {
   my ($mark) = @_;
   for my $i (reverse 0 .. $#letters_marks) {
       return $letters[$i] if $mark >= $letter_marks[$i];
   }
   die "Invalid input";
}
于 2013-04-28T05:30:44.747 回答