所以我想做一些简单的事情:
printfn "Enter a number:"
try
let x = System.Console.ReadLine();
Some(int32(x))
with
| :? System.FormatException -> printfn "Invalid number!"
Some(0)
我想打印消息,然后让用户输入一个数字,并尝试将其转换为 int 并返回它。
如果我只是编译代码(通过在命令行上键入 fsc a3.fs),它工作正常。它暂停,等待输入,然后返回 Some(int)。
如果我在命令行上将代码复制并粘贴到 FSI 中,效果会很好。
但是,当我在 Visual Studio 中并在 FSI 中运行代码时(突出显示 + alt+enter),它正好越过输入部分,异常被抛出(并被捕获)。
这是我在 FSI(在 Visual Studio 中)运行时的输出:
Enter a number:
Invalid number!
0
如您所见,它实际上并没有暂停并等待我输入输入。
任何人都知道如何使这项工作?
谢谢!
最佳答案
如果您将 F# interactive 作为自己的进程打开(通过直接运行 fsi.exe
,代码工作正常 - 这是发生在我身上的事情:
> printfn "Enter a number:"
- let r =
- try
- let x = System.Console.ReadLine();
- Some(int32(x))
- with
- | :? System.FormatException -> printfn "Invalid number!"
- Some(0)
- ;;
Enter a number:
5
val r : int32 option = Some 5
关于visual-studio - 如何在 Visual Studio 中使用 f# interactive 读取用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8022512/