列出总错误和百分比?

时间:2013-04-25 02:56:22

标签: perl

好的,对于我正在做的学校项目,我在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";
}

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

1 个答案:

答案 0 :(得分: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这样的裸字来比较文字。将字符串equals运算符(eq)与字符串一起使用。像$q1 eq 'A'一样。您也可以使用else块而不是与其他所有块进行比较。

如果您要获得固定数量的问题,实际上只需要一个变量来正确回答问题的数量,因为$incorrect将与(total questions) - $correct相同,$correct + $incorrect 1}}将永远是问题的总数。