我第一次练习F#编程,我想知道在语言中是否有类似于C#中的开关/Case命令的命令?我希望确保优化程序,使它们不必每次都被重置,以尝试程序的不同路径(例如,创建三种区域计算器,并且必须重新设置每一种方法)。
编辑:我完全忘记了将代码作为示例显示。这就是我想要尝试和编辑的。
```module AreaCalculator =```javascript“打印”-“
输入1以找到矩形的区域。
输入2以找到一个圆圈的区域。
输入3以找到三角形的区域。
在这里输入:
设stringInput = System.Console.ReadLine()
如果stringInput = "1“那么
“矩形的长度:”
设rlString = System.Console.ReadLine()
“矩形的宽度:”
设rwString = System.Console.ReadLine()
设rlInt = rlString |> int
设rwInt = rwString |> int
设rectArea = rlInt * rwInt
“矩形的面积是:%i”rectArea
然后elif stringInput = "2“
设PI = 3.14156
圆圈的半径是多少?
设radiusString = System.Console.ReadLine()
让radiusInt = radiusString |>浮动
设cirlceArea = (radiusInt radiusInt) PI
圆圈的面积是:%f“cirlceArea
然后elif stringInput = "3“
“三角形的底座是什么?”
设baseString = System.Console.ReadLine()
三角形的高度是多少?
设heightString = System.Console.ReadLine()
设baseInt = baseString |> int
设heightInt = heightString |> int
设triArea = (heightInt * baseInt)/2
“三角形的面积是:%i”triArea
否则
“请再试一次”
它工作得很好,但我想看看我是否可以重做程序,这样它就不必每次你想要计算不同形状的面积时都要重置。我尝试过让stringInput成为可变变量,就像我在演示中看到的那样,但是它只会导致这样的错误:
/home/runner/F-Practice-1/main.fs(59,5):error FS0588:这个'let‘后面的块未完成。每个代码块都是一个表达式,必须有一个结果。“让”不能成为块中的最终代码元素。考虑给这个块一个显式的结果。
我能做些什么来补救这个问题?
发布于 2021-03-27 16:10:07
F#中的等价结构称为match。
let stringInput = System.Console.ReadLine()
match stringInput with
| "1" ->
printfn "What is the rectangle's length: "
// ...
| "2" ->
printfn "What is the circle's radius: "
// ...
| "3" ->
printfn "What is the triangle's base: "
// ...
| _ ->
printfn "Please try again"更多细节这里。
https://stackoverflow.com/questions/66822086
复制相似问题