-5

对于科学课上的一个项目,我正在用 Perl 制作一个游戏。这是我的游戏介绍

# Solar Game
# Copyright (c)2013 Donovan Roudabush
# Protected by GPU

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;

现在,使用$num值(存储玩家数量),我如何打印出下面的行以获取 (x) 数量的玩家来收集他们的名字?

print "Please enter your name\n";
$name1 = <STDIN>;  # Name1 can also be Name 2 or Name3 for second or third player
chomp $name1;
4

1 回答 1

2

使用循环,您可以将名称存储在数组中:

my @names;
for (1 .. $num) {
    print "Please enter your name\n";
    my $name = <STDIN>;
    chomp $name;
    push @names, $name;
}
于 2013-04-23T18:48:58.197 回答