12

请告诉我在 F# 中运行程序时如何暂停控制台窗口。

open System
let myList = [0..9]
let myFunction =
for n in myList do
    Console.WriteLine(n)
myFunction
4

3 回答 3

16

我猜您希望控制台在程序执行完成后显示输出。

您可以将此行放在代码段的末尾

Console.ReadKey() |> ignore

在这个意义上“暂停”控制台。

于 2013-03-09T07:14:27.297 回答
1
// When running in debug mode and using Visual Studio to run the program,  
// one may miss the results as the program runs to the end and exists.  
// Since running normally, i.e. Visual Studio Ctrl-F5, will add an pause
// automatically the pause is only shown when in debug mode.  
let pause () =  
  match System.Diagnostics.Debugger.IsAttached with  
  | true ->  
      printfn "\nPress any key to continue."  
      System.Console.ReadKey(true) |> ignore  
  | false -> ()  

pause ()  
于 2013-03-09T16:37:57.530 回答
1

您可能会考虑在编译器指令中包装暂停函数,因为您可能不希望在发布代码中具有相同的效果。

(* your code here *)
#if DEBUG
System.Console.ReadKey(true) |> ignore 
#endif
于 2014-06-14T01:37:09.000 回答