2013-03-16 111 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