F# 异常处理 : how to parse number in ReadLine()?

标签 f#

我正在尝试从命令行输入中获取输入数字:

let mutable guess = Console.ReadLine() |> System.Int32.Parse

只要输入是数字,这就可以正常工作;除非输入是字母字符串,例如 kdf,程序将退出并返回 System.TypeInitilizationException

如何处理控制台输入的字符串?

更新

我正在尝试用 F# 编写猜谜游戏。这是整个程序:

open System

printfn "Guess the number"

let secretNum = System.Random().Next(1,101)

let mutable continueLooping = true 

while continueLooping do

    printfn "Please input your guess."

    let mutable input = Console.ReadLine() 

    let result =
        match Int32.TryParse input with
        | (true, result) -> Some(result) 
        | (false, _) -> printfn "Please input a number!"; None

    let guess = Option.get result

    printfn "You guessed: %A" guess

    if guess < secretNum then printfn "Too small!"
    else if guess > secretNum then printfn "Too big!"
    else do 
        printfn "You win!" 
        continueLooping <- false



[<EntryPoint>]
let main argv = 
    printfn "%A" argv
    0 // return an integer exit code

除了无法处理无效输入问题外,该程序可以正常工作。我是编程新手,也许这对 .NET 程序员来说是一个很明显的问题。恐怕初学者经常有愚蠢的问题。

最佳答案

我经常这样做

match System.Int32.TryParse input with
| (true, number) -> ...
| (false, _)     -> ....

对于 .net 中的所有 TryXY 模式也很有魅力 ;)

例子

F# Interactive for F# 3.1 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License

For help type #help;;

> let tryInt input = match System.Int32.TryParse input with | (true, number) -> Some number | _ -> None;;

val tryInt : input:string -> int option

> tryInt "55";;
val it : int option = Some 55
> tryInt "no";;
val it : int option = None
>    

备注

我的猜测是您想查询您的用户,直到他输入一个有效的整数。所以你可以使用我刚刚给你的函数通过一个递归类循环函数来完成这个:

let rec queryAnInt () =
    printf "please input an integer " 
    let input = System.Console.ReadLine()
    match System.Int32.TryParse input with
    | (true, number) -> number
    | _              -> printfn "sorry - you did not enter an integer"
                        queryAnInt ()

现在 queryAnInt () 将询问用户一个整数,直到他给了一个整数并返回它(没有 Option 你似乎有问题)

你的程序

以下是我在您的程序中看到的一些内容:

  • 首先是显而易见的事情:您将所有内容都编码到模块中,当您真的想在 main 方法和函数中使用它时 - 这将让你快速解决问题

  • 你将猜测匹配到 result 作为 Option 只是为了在下一行 Option.get resultresultNone 时会抛出错误(当用户没有输入数字时)——这才是真正的问题

  • 您使用全局可变变量和命令式循环 - 两者都是函数式代码味道

一个简单的重写(没有丑陋的可变)看起来像这样:

open System

let rec queryGuess () =
    printf "please input your guess " 
    let input = Console.ReadLine()
    match System.Int32.TryParse input with
    | (true, number) when number >= 1 && number <= 100
                     -> number
    | _              -> printfn "sorry - please enter a number between 1 and 100"
                        queryGuess ()

let rec guess secret nrTriesLeft =
    if nrTriesLeft = 0 then printfn "Sorry you lost" else
    match queryGuess () with
    | g when g < secret ->
        printfn "Too small!"
        guess secret (nrTriesLeft - 1)
    | g when g > secret ->
        printfn "Too big!"
        guess secret (nrTriesLeft - 1)
    | g when g = secret ->
        printfn "You win!"
    | _ -> failwith "impossible case" 

let game () = 
    printfn "Guess my secret number - it's between 1 and 100"

    let secretNum = System.Random().Next(1,101)

    guess secretNum 7 // 7 tries should always be enough - bonus question: why?

[<EntryPoint>]
let main _ = 
    game ()
    0

通过这种方式,您可以很容易地扩展到让我们说也只有一定数量的重试(只需稍微更改 guess 函数 - 您应该尝试一下!)

我希望这最终能帮助您解决语法问题

关于F# 异常处理 : how to parse number in ReadLine()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30460487/

相关文章:

c# - 使用静态类型语言 (F#) 处理异构数据

F#,赋值运算符与集合运算符

c# - 如何在 F# 中抛出带有有意义消息的异常?

f# - 如何使 Monodevelop 和 F# 工作

.net - 有没有明显的方法来确认一个函数是否是尾递归的?

xml - F# XML 解析

f# - MailboxProcessor.PostAndReply 设计选择

f# - 如何判断 F# 函数是否为纯函数?

f# - F# 和 OCaml 等函数式语言中的 "let"关键字有什么用?

复杂 json 上的 Json.net JObject.Parse 错误