2013-03-16 112 views
2

有人可以帮我一个循环请。我打算写一个程序,它只是简单地要求你猜一个1到10之间的数字。如果这不是正确的答案,你会得到另一个机会,等等。试图写一个简单的循环

我可以让我的脚本打印正确/不正确有一次,但是如何在这个脚本中添加一个用户再次尝试的可能性(直到他们猜出正确的数字)?

这是我的基本脚本,我敢肯定它非常简单,可能有很多错误。有人能帮我解决这个简单的问题吗?

对不起,布局不好,但我不明白如何把我的脚本放在这个网站上,对不起!

use strict; 
use warnings; 

print "Hello, I've thought of a number, do you know what number it is?\n"; 
sleep (1); 
print "Try and guess, type in a number between 1 and 10!\n"; 
my $div = <STDIN>; 
my $i = 0; 
my $int = int(rand (10)) + 1; 
chomp $div; 
if ($div < $int) { 
    print ("The number I though of is higher than $div, try again?\n"); 
} 

if ($div > $int) { 
    print ("The number I though of is lower that $div, try again?\n"); 
} 

if ($div == $int) { 
    print ("Amazing, you've guessed mt number\n"); 
} 
+1

你没有循环,虽然你似乎知道你需要一个循环。你想知道什么?循环的语法? – Ingo 2013-03-16 12:24:25

+0

我要去尝试直到循环,看看我是否需要这些。我想是的。 – joesh 2013-03-16 13:19:19

+1

我最近在[codereview](http://codereview.stackexchange.com/a/23556/21609)上展示了一个功能丰富的“猜我的号码”的实现。你可能想要在那里寻找灵感。 – amon 2013-03-16 13:38:24

回答

2

更简单的方法将是一个while loop

use strict; 
use warnings; 

print "Hello, I've thought of a number, do you know what number it is?\n"; 
sleep (1); 
my $int = int(rand (10)) + 1; 
print "Try and guess, type in a number between 1 and 10!\n"; 

while (my $div = <STDIN>) { 
    chomp $div; 
    if ($div < $int) { 
     print "The number I though of is higher than $div, try again?\n"; 
    } 
    elsif ($div > $int) { 
    print "The number I though of is lower that $div, try again?\n"; 
    } 
    else { 
    print "Amazing, you've guessed mt number\n"; 
    last; 
    } 
} 

虽然(双关语意),你的代码已经很不错(您正在使用strictwarnings,有没有语法错误,耶为!)有一些事情我变了,一些在那里我会建议改进。

但首先,让我们看看循环。只要条件成立,该程序就会保留在while循环中。由于用户可以输入的所有内容(甚至是空行)被Perl认为是真的,这是永远的。这是好的,因为有条件退出循环。它位于ifelse部分。 last声明告诉Perl退出循环。如果else未执行,则会返回到while块的起始位置,用户必须重试。永远。

的变化我做: - 你不需要$i因为你没有使用它 - 您使用了三个独立的if语句。由于只有三个条件之一可以在这种情况下是正确的,我合并他们到一个 - 不需要的括号()print

建议: - 您应该为您的变量,他们做了什么,而不是他们是。 $int不是一个好名字。我会去$random,甚至$random_number。如果您稍后必须返回代码,则详细程度非常重要。 - 有一个function called say,您可以使用use feature 'say';启用。它增加了say "stuff",相当于print "stuff\n"


编辑:

如果你想补充一点,不直接与用户输入到其他一些条件,你可以添加其他if

while (my $div = <STDIN>) { 
    chomp $div; 

    if ($div eq 'quit') { 
    print "You're a sissy... the number was $int. Goodbye.\n"; 
    last; 
    } 

    if ($div < $int) { 
     print "The number I though of is higher than $div, try again?\n"; 
    } 
    elsif ($div > $int) { 
    print "The number I though of is lower that $div, try again?\n"; 
    } 
    else { 
    print "Amazing, you've guessed mt number\n"; 
    last; 
    } 
} 

您还可以添加一个检查以确保用户输入了一个数字。如果输入单词或字母,您当前的代码将会产生警告。要做到这一点,你需要一个正则表达式。请在perlre上阅读。 m//是与=~一起工作的match operator\D匹配任何不是数字(0到9)的字符。 next步骤while块的其余部分,并开始检查while条件。

while (my $div = <STDIN>) { 
    chomp $div; 

    if ($div =~ m/\D/) { 
    print "You may only guess numbers. Please try again.\n"; 
    next; 
    } 

    # ... 
} 

因此,完整的检查手段“看东西的用户输入,如果有任何东西比一个数字都抱怨,让他再试一次”。

+0

太棒了,我 - 不知道它 - 比我意识到的更近。非常感谢你。我不得不在大学课堂上学习Perl,有时当你明白自己该做什么时会感到有点沮丧,但不知道你需要完成什么“命令”。你帮我完美地解决了我的问题,非常感谢。我会再次重新阅读您的意见,以便在此循环中真正清楚。 – joesh 2013-03-16 13:39:16

+0

只是一些意见和问题。 a)'int'是我要使用的东西,然后忘记取出 - 我的错误。 b)你可以多次使用'if'或'elsif'吗?我注意到,正如你所解释的那样,我并不需要所有'if'命令。然而,假设我想添加另一个因素,例如“如果玩家在10次尝试后没有猜出......”,则退出循环。我会添加另一个'if'或'elsif'吗? c)是的,你完全正确地补充说我应该正确地命名我的变量。该程序最初是用法语写的,因此变量的命名是不好的(或混淆)。 – joesh 2013-03-16 13:57:31

+0

你去哪所大学提供Perl?我建议你获得Randal L. Schwartz的* [Learning Perl](http://shop.oreilly.com/product/0636920018452.do)*书。这对初学者来说是最好的。无论你得到哪本书,都要确保它是最新版本。那里有很多旧的,过时的东西。此外,无视任何教程,你会发现它没有'strict'和'warnings',或者没有用'my'声明变量。这些都很古老,而且最好被忽略。请参阅http://perl-tutorial.org/获取高质量教程列表。 – simbabque 2013-03-16 13:57:54

2

使用,直到环

my $guessed = 0; 
do { 
    print "Try and guess, type in a number between 1 and 10!\n"; 

    my $div = <STDIN>; 

    ...; 

    if ($div == $int) { 

     print ("Amazing, you've guessed mt number\n"); 
     $guessed = 1; 

    } 
} until ($guessed) 
+0

太好了,我会试试看看我能做些什么。如果我理解正确,我会放弃我原来的脚本,这可能很笨拙? – joesh 2013-03-16 13:18:26

+0

好的,我又回来了。你能解释一下,我在哪里放置这个循环。非常感谢。 – joesh 2013-03-16 13:33:40