我想在F#中構建一個計算器。所以,我從用戶那裏獲得關於要執行的操作的輸入。對於輸入6,它應該顯示科學操作的菜單,但是,它表示表達式預期具有類型單位,但是具有浮點類型。並且在scientificFun()函數中,對於最後一行,它表示'表達式預期會浮動,但這裏有單位'。我不確定這意味着什麼。一直堅持了幾個小時。任何幫助,將不勝感激。謝謝!。 **或粗體線顯示發生錯誤的位置。F#錯誤:表達式預計有類型單元
open System
let mutable ok = true
while ok do
Console.WriteLine("Choose a operation:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n5.Modulo\n6.Scientific")
let input= Console.ReadLine()
let add() =
Console.WriteLine("Ok, how many numbers?")
let mutable count = int32(Console.ReadLine())
let numberArray = Array.create count 0.0
for i in 0 .. numberArray.Length - 1 do
let no = float(Console.ReadLine())
Array.set numberArray i no
Array.sum numberArray
let expo() =
Console.WriteLine("Enter the base")
let getBase = Console.ReadLine()
Console.WriteLine("Enter the exponent")
let getExponent = Console.ReadLine()
float(getBase) ** float(getExponent)
let sqRoot() =
Console.WriteLine("Enter a number")
let no = float(Console.ReadLine())
Math.Sqrt no
let rec fact (n:float) =
if n < 1.0 then 1.0
else n * fact (n - 1.0)
let factorial() =
Console.WriteLine("Enter a number")
let no = float(Console.ReadLine())
fact(no)
let Trigsin() =
Console.WriteLine("Enter an angle")
let angle = float(Console.ReadLine())
Math.Sin angle
let Trigcos() =
Console.WriteLine("Enter an angle")
let angle = float(Console.ReadLine())
Math.Cos angle
let Trigtan() =
Console.WriteLine("Enter an angle")
let angle = float(Console.ReadLine())
Math.Tan angle
let logicalAnd() =
Console.WriteLine("Enter first number")
let first = int32(Console.ReadLine())
Console.WriteLine("Enter second number")
let second = int32(Console.ReadLine())
float(first &&& second)
let logicalOr() =
Console.WriteLine("Enter first number")
let first = int(Console.ReadLine())
Console.WriteLine("Enter second number")
let second = int(Console.ReadLine())
float(first ||| second)
let logicalNot()=
Console.WriteLine("Enter a number")
let first = int32(Console.ReadLine())
float(~~~first)
let sub x y = x - y
let mul x y = x * y
let div x y = x/y
let MOD x y = x % y
let scientificFun() =
printfn("1.Exponential\n2.Square Root\n3.Factorial\n4.sin()\n5.cos()\n6.tan()\n7.AND\n8.OR\n9.NOT")
let scientificInput = Console.ReadLine()
match scientificInput with
|"1" -> expo()
|"2" -> sqRoot()
|"3" -> factorial()
|"4" -> Trigsin()
|"5" -> Trigcos()
|"6" -> Trigtan()
|"7" -> logicalAnd()
|"8" -> logicalOr()
|"9" -> logicalNot()
| _ -> **printfn("Choose between 1 - 9")**
match input with
| "1" -> printfn("The Result is: %f") (add())
//| "2" -> printfn("The Result is: %f") (sub A B)
//| "3" -> printfn("The Result is: %f") (mul A B)
///| "4" -> printfn("The Result is: %f") (div A B)
//| "5" -> printfn("The Result is: %f") (MOD A B)
| "6" -> **scientificFun()**
| _-> printfn("Choose between 1 and 6")
Console.WriteLine("Would you like to use the calculator again? y/n")
let ans = Console.ReadLine()
if ans = "n" then
ok <- false
else Console.Clear()
請張貼[最小,完整的,並且可驗證示例](http://stackoverflow.com/help/mcve)。 –
模式匹配的所有返回值必須是相同的類型。 如果匹配'scientificInput',則所有函數都會返回'float', 除了'printfn',它返回'unit'。 – Funk
Thanks @ Funk ...你可以給我一個關於我如何做這項工作的想法。按6之後,我想要顯示科學菜單 –