初学者:使用 while 循环和错误重复

发布于 2024-11-16 06:29:58 字数 1954 浏览 0 评论 0原文

好的,所以不确定 apple.stackexchange 是否是一个更好的地方,但我需要一些有关此代码的帮助:

目标: 要求用户输入循环应重复的次数。如果他们以错误的格式输入,请发送反馈

问题如果我输入小数,它只会将其视为整数并且仍然有效,我该如何防止这种情况,或以其他方式检查?

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"

Is There a better way?甚至是内置的“积极”呼叫?

最后,我使用 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 语句不起作用?

Okay, so not sure if apple.stackexchange is a better place for this, but I need some help with this code:

Objective: Ask user to enter amount of times the loop should be repeated. Send feedback if they type it in the wrong format

Problem If I type in an decimal, it just takes it as an integer and still works, how can I prevent this, or check it another way?

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

Also, to check if a negative number was entered, I would just use

if reps > 0
    set reps to reps as integer
    set correctEntry to true
else
    display dialog "Must be positive"

Is there a better way? Even a built in "positive" call?

Finally, I am using http://www.fischer-bayern.de/as/as4as/AS4AS_e.pdf as tutorial, however I'm not sure if it is good enough. I was thinking of going to Mac OS X Developer Library, but would like some input on a good way to learn. I have just a bit of Perl experience, so it doesn't have to be for a complete beginner.

Update: Found this site: http://www.macosxautomation.com/training/applescript/intro.html

Thanks a lot for any help you can provide.

Update2: The code works if I use the same check for an integer in the second try block, what's wrong with the first. Why does it count everything as an integer? More importantly, why does using the if statement suggested not work if put in my script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2024-11-23 06:29:58

试试这个...

repeat
    set textToDisplay to "How often should this repeat?"
    set reps to text returned of (display dialog textToDisplay default answer "2.4")

    try
        set reps to reps as number -- this makes sure no letters were entered
        if (class of reps) is integer and reps is greater than 0 then
            exit repeat
        else
            error "Only positive integers"
        end if
    on error theError number errorNumber
        display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
    end try
end repeat

repeat reps times
    say "You entered right"
end repeat

为了学习这里有一些教程。当我了解后,我做了“初级脚本编写者教程”部分中的内容。随着您的进步,还有更高级的教程。最后,您还必须学习 AppleScript 编辑器中“帮助”菜单下的“Applescript 语言指南”。

Try this...

repeat
    set textToDisplay to "How often should this repeat?"
    set reps to text returned of (display dialog textToDisplay default answer "2.4")

    try
        set reps to reps as number -- this makes sure no letters were entered
        if (class of reps) is integer and reps is greater than 0 then
            exit repeat
        else
            error "Only positive integers"
        end if
    on error theError number errorNumber
        display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
    end try
end repeat

repeat reps times
    say "You entered right"
end repeat

For learning here's some tutorials. When I learned I did the ones in the "Tutorials for Beginning Scripters" section. There's more advanced tutorials as you get better too. Last, you have to also study the "Applescript Language Guide" which is under the Help menu in AppleScript Editor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文