appa_09.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 90 行
HTM
90 行
<html><head><title>Answer to Chapter 10 Exercise (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appa_08.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="appa_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">A.9. Answer to Chapter 10 Exercise</h2><ol><li><p>Here's one way to do it:</p><blockquote><pre class="code">my $secret = int(1 + rand 100);# This next line may be un-commented during debugging# print "Don't tell anyone, but the secret number is $secret.\n";while (1) { print "Please enter a guess from 1 to 100: "; chomp(my $guess = <STDIN>); if ($guess =~ /quit|exit|^\s*$/i) { print "Sorry you gave up. The number was $secret.\n"; last; } elsif ($guess < $secret) { print "Too small. Try again!\n"; } elsif ($guess == $secret) { print "That was it!\n"; last; } else { print "Too large. Try again!\n"; }}</pre></blockquote><p>The first line picks out our secret number from <tt class="literal">1</tt>to <tt class="literal">100</tt>. Here's how it works. First,<tt class="literal">rand</tt> is Perl's random number function, so<tt class="literal">rand 100</tt> gives us a random number in the rangefrom zero up to (but not including) <tt class="literal">100</tt>. That is,the largest possible value of that expression is something like<tt class="literal">99.999</tt>.<a href="#FOOTNOTE-397">[397]</a> Adding one gives a numberfrom <tt class="literal">1</tt> to <tt class="literal">100.999</tt>, then the<tt class="literal">int</tt> function truncates that, giving a result from<tt class="literal">1</tt> to <tt class="literal">100</tt>, as we needed.</p><blockquote class="footnote"> <a name="FOOTNOTE-397" /><p>[397]The actual largestpossible value depends upon your system; see <a href="http://www.cpan.org/doc/FMTEYEWTK/random">http://www.cpan.org/doc/FMTEYEWTK/random</a> ifyou really need to know.</p> </blockquote><p>The commented-out line can be helpful during development anddebugging, or if you like to cheat. The main body of this program isthe infinite <tt class="literal">while</tt> loop. That will keep asking forguesses until we execute <tt class="literal">last</tt>.</p><p>It's important that we test the possible strings before thenumbers. If we didn't, do you see what would happen when theuser types <tt class="literal">quit</tt>? That would be interpreted as anumber (probably giving a warning message, if warnings were turnedon), and since the value as a number would be zero, the poor userwould get the message that their guess was too small. We might neverget to the string tests, in that case.</p><p>Another way to make the infinite loop here would be to use a nakedblock with <tt class="literal">redo</tt>. It's no more or lessefficient; merely another way to write it. Generally, if you expectto mostly loop, it's good to write <tt class="literal">while</tt>,since that loops by default. If looping will be the exception, anaked block may be a better choice.</p></li></ol><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appa_08.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="appa_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">A.8. Answers to Chapter 9 Exercises</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">A.10. Answers to Chapter 11 Exercises</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?