我正在尝试从命令行输入获取输入数字:
let mutable guess = Console.ReadLine() |> System.Int32.Parse
Run Code Online (Sandbox Code Playgroud)
只要输入是一个数字,这就可以正常工作; 例如,当输入是字母字符串时,kdf程序将以a退出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
Run Code Online (Sandbox Code Playgroud)
除了无法处理无效输入问题外,该程序可以正常工作.我是编程新手,也许这对.NET程序员来说是一个非常明显的问题.我担心初学者经常会有愚蠢的问题.
我经常这样做
match System.Int32.TryParse input with
| (true, number) -> ...
| (false, _) -> ....
Run Code Online (Sandbox Code Playgroud)
就像TryXY.net中所有模式的魅力一样;)
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
>
Run Code Online (Sandbox Code Playgroud)
我的猜测是你要查询你的用户,直到他输入一个有效的整数.所以你可以使用我刚刚给你的函数用一个类似递归循环的函数来做到这一点:
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 ()
Run Code Online (Sandbox Code Playgroud)
现在queryAnInt ()将询问用户一个整数,直到他给出一个并返回它(没有Option你似乎有问题)
以下是我在程序中看到的一些内容:
首先是显而易见的事情:你把所有东西编码到模块中,当你真的希望在main方法和函数中都有它时- 这会让你很快遇到问题
你把猜测result作为一个Option唯一的东西扔掉你在下一行中获得的所有东西,Option.get result这会在result当时None(当用户没有输入数字时)抛出错误- 这是你真正的问题
你使用全局可变变量和命令循环 - 两者都是功能代码气味
一个简单的重写(没有丑陋的可变)可能看起来像这样:
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
Run Code Online (Sandbox Code Playgroud)
通过这种方式,你可以很容易地说只有一定数量的重试(只需guess稍微更改一下这个功能 - 你应该尝试一下!)
我希望这最终可以帮助您处理语法问题