-2

好的,对于我正在做的一个学校项目,我正在用 Perl 创建一个小测验,

#########################################
# Solar Game                            #
#                                       #
# (C) 2013 Donovan Roudabush            #
# GPU/Creative Commons                  #
#                                       #
# sharksfan98@gmail.com                 #
# github.com/sharksfan98/solargame      #
#########################################

print "  _________      .__                   ________                       \n";
print " /   _____/ ____ |  | _____ _______   /  _____/_____    _____   ____  \n";
print " \_____  \ /  _ \|  | \__  \\_  __ \ /   \  ___\__  \  /     \_/ __ \ \n";
print " /        (  <_> )  |__/ __ \|  | \/ \    \_\  \/ __ \|  Y Y  \  ___/ \n";
print "/_______  /\____/|____(____  /__|     \______  (____  /__|_|  /\___  > \n";
print "        \/                 \/                \/     \/      \/     \/ \n";
print "Version 1.0 Beta\n\n";
print "Developed by Donovan Roudabush\n";
print "https://github.com/sharksfan98/solargame\n\n";
print "Press enter to start\n";
$ok = <STDIN>;
chomp $ok;
print "Enter the number of players\n";
$num = <STDIN>;
chomp $num;

my @names;
for (1 .. $num) {
    print "Please enter your name\n";
    my $name = <STDIN>;
    chomp $name;
    push @names, $name;
}

print "Hello, Players! Have you played before? (Y/N)\n";
$exp = <STDIN>;
chomp $exp;

if ($exp == Y) {
    print "Ok! The game will begin.\n";
    print "Press Enter to begin the questions\n\n";
}

if ($exp == N) {
    print "Then I will explain the rules\n";
    $ok2 = <STDIN>;
    chomp $ok2;
}   # Add rules here later

print "Part 1: Measurements\n\n";
print "Question 1\n";
print "What measurement is used to measurements within our Solar System?\n\n";
print "A) Astronomical Unit (AU)\n";
print "B) Light Year\n";
print "C) Parsec\n";
$q1 = <STDIN>;
chomp $q1;

if ($q1 == A) {
    print "Correct!\n\n";
}

if ($q1 == B) {
    print "Wrong!\n\n";
}

if ($q1 == C) {
    print "Close! The Parsec is only used by Professionals, like NASA\n\n";
}

print "Question 2\n\n";
print "What do you use to measure farther objects, like stars and galaxies?\n";
print "A) Astronomical Unit (AU)\n";
print "B) Light Year\n";
print "C) Parsec\n";
$q2 = <STDIN>;
chomp $q2;

if ($q2 == A) {
    print "Wrong!\n\n";
}

if ($q2 == B) {
    print "Correct!\n\n";
}

if ($q2 == C) {
    print "Wrong!\n\n";
}

print "Question 3\n";
print "Which measurement would you use to measure Earth to the Sun?\n\n";
print "A) Astronomical Unit (AU)\n";
print "B) Light Year\n";
print "C) Parsec\n";
$q3 = <STDIN>;
chomp $q3;

if ($q3 == A) {
    print "Correct!\n\n";
}

if ($q3 == B) {
    print "Wrong!\n\n";
}

if ($q3 == C) {
    print "Wrong!\n\n";
}

print "Question 4\n";
print "Which measurement would you use measure the Sun to Polaris?\n\n";
print "A) Astronomical Unit (AU)\n";
print "B) Light Year\n";
print "C) Parsec\n";
$q4 = <STDIN>;
chomp $q4;

if ($q4 == A) {
    print "Correct!\n\n";
}

if ($q4 == B) {
    print "Wrong!\n\n";
}

if ($q4 == C) {
    print "Wrong!\n\n";
}

print "Question 5\n";
print "Which would you use to measure Earth to Uranus?\n\n";
print "A) Astronomical Unit (AU)\n";
print "B) Light Year\n";
print "C) Parsec\n";
$q5 = <STDIN>;
chomp $q5;

if ($q5 == A) {
    print "Correct!\n\n";
}

if ($q5 == B) {
    print "Wrong!\n\n";
}

if ($q5 == C) {
    print "Wrong!\n\n";
}

在我的代码末尾,我想计算并列出正确和错误的问题数量,以及正确的百分比。我该怎么做呢?

4

1 回答 1

2

您可以有一个变量,每次正确回答问题时都会增加一个变量,并且每次错误回答问题时都会增加一个变量。

my $correct = 0;
my $incorrect = 0;

...

if ($q1 eq 'A') {
    print "Correct!\n\n";
    $correct++;
} else {
    print "Wrong!\n\n";
    $incorrect++;
}

print "Percentage: ", int($correct/($correct + $incorrect) * 100 + 0.5), "%\n";

您不能使用像 A 这样的裸词来比较文本。对字符串使用字符串等于运算符 (eq)。喜欢$q1 eq 'A'。您也可以使用else块而不是与其他所有内容进行比较。

如果您将有固定数量的问题,您实际上只需要一个变量来表示正确回答的问题数量,因为 then$incorrect将与 相同(total questions) - $correct,并且$correct + $incorrect始终是问题的总数。

于 2013-04-25T03:29:08.713 回答