F#添加到可变列表

时间:2017-04-26 19:40:55

标签: f#

我正在为大学项目编写一个基于控制台的计算器应用程序,并且遇到了一些错误。

当用户从菜单中选择一个选项(1,2,3,4等)时,他们可以输入2个数字,并加上,减去,除以或乘以和。在解决并显示每个成功的总和后,我希望将它们添加到我的列表sumHistory

这是我目前的代码,我觉得它应该可行,但我显然遇到了一些小而愚蠢的东西,阻止列表被告知时显示!

let sumHistory = new List<string>()

match input with
|"1" -> Console.WriteLine("Please enter 2 integers: ")
        let a= Console.ReadLine()
        let b= Console.ReadLine()
        let A: int = int32 a
        let B: int = int32 b
        let C = (add A B)
        let D = (string A + " + " + string B + " = " + string C)
        Console.WriteLine(D)
        sumHistory.Add(D)      
|"2" -> Console.WriteLine("Please enter 2 integers: ")
        let a= Console.ReadLine()
        let b= Console.ReadLine()
        let A: int = int32 a
        let B: int = int32 b
        let C = (sub A B)
        let D = (string A + " - " + string B + " = " + string C)
        Console.WriteLine(D)
        sumHistory.Add(D)  
|"3" -> Console.WriteLine("Please enter 2 integers: ")
        let a= Console.ReadLine()
        let b= Console.ReadLine()
        let A: int = int32 a
        let B: int = int32 b
        let C = (div A B)
        let D = (string A + " / " + string B + " = " + string C)
        Console.WriteLine(D)
        sumHistory.Add(D)  
|"4" -> Console.WriteLine("Please enter 2 integers: ")
        let a= Console.ReadLine()
        let b= Console.ReadLine()
        let A: int = int32 a
        let B: int = int32 b
        let C = (mul A B)
        let D = (string A + " * " + string B + " = " + string C)
        Console.WriteLine(D)
        sumHistory.Add(D)  
|"5" -> sumHistory |> Seq.iteri (fun index item -> printfn "%i: %s" index sumHistory.[index])
|"6" -> let data = ReadInText()
        data.Read()

1 个答案:

答案 0 :(得分:0)

<强> [解决]

在实际阅读我自己的代码后,我意识到这只是一个愚蠢的错误。

List已在while内声明,因此在每次总结后重新启动清除!

let sumHistory = new List<string>()移动到名称空间声明下面的文档顶部解决了这个问题。