好了,所以不知道apple.stackexchange是這一個更好的地方,但我需要一些幫助,這代碼:初學者:與重複while循環和錯誤
目的:要求用戶輸入的金額次循環應該重複。發送反饋,如果他們在錯誤的格式
問題鍵入它如果我在一個小數類型,只是把它作爲一個整數,仍然有效,我怎樣才能避免這種情況,或者檢查一下另一種方式?
set correctEntry to false --initially assume false
repeat while correctEntry is false
--Let user put how many times it loops
set textToDisplay to "How often should this repeat?"
display dialog textToDisplay default answer "2.4"
set reps to text returned of the result
--Detailed check/feedback if they input wrong
try
--Begins as string, try making it an integer
set reps to reps as integer --coercion will should only work with integer
set correctEntry to true --remember won't get here if first statement fails
on error
try
--See if string can at least be converted to a number
set reps to reps as number
display dialog "Only integers"
on error
display dialog "That wasn't a number"
end try
-- still false here
end try
end repeat
--Only gets here if correctEntry becomes true
repeat reps times
say "You entered right"
end repeat
此外,檢查是否輸入了負數,我只想用
if reps > 0
set reps to reps as integer
set correctEntry to true
else
display dialog "Must be positive"
有沒有更好的辦法?即使是建立在「積極」的電話?
最後,我使用http://www.fischer-bayern.de/as/as4as/AS4AS_e.pdf作爲教程,但我不確定它是否足夠好。我正在考慮去Mac OS X開發者庫,但希望有一些很好的學習方法。我只是有一點Perl的經驗,所以它不一定是一個完整的初學者。
更新:發現這個網站:http://www.macosxautomation.com/training/applescript/intro.html
非常感謝任何幫助,您可以提供。
Update2:如果我在第二個try塊中使用同一個整數檢查,那麼代碼將起作用,第一個錯誤是什麼。爲什麼它將一切都算作整數?更重要的是,爲什麼使用if語句提示如果放入我的腳本中不起作用?
太棒了,這工作,我喜歡使用括號。我的問題是爲什麼我的嘗試的第一步跳過,即使我使用你的if語句,它仍然是「使用整數」。如果我把你的if語句放在「錯誤」部分,它工作正常嗎?感謝Tuts – Jon