2017-03-28 33 views
1

I'm trying to get a number from a user. That number is then used to call another function randomList(n) which takes the given number and uses it. I keep getting the error exception option . I've read that adding SOME to the variable declaration or valOf can fix this issue, but it is not working for me. What do I need to change?如何獲得一個整數作爲用戶輸入的SML

fun getNumber() = 
print "Please enter the number of integers: "; 
let 
    val str = valOf (TextIO.inputLine TextIO.stdIn) 
    val i : int = valOf (Int.fromString str) 
    in 
    randomList(i) 
end; 
getNumber(); 

回答

0

這裏的問題是,fun getNumber()只包括以下行的print聲明。如果您希望它們都是getNumber()的一部分,則需要在括號內附上printlet

例如,下面的代碼編譯和回聲經由stdin通過輸入整數

fun getNumber() = (
print "Please enter the number of integers: "; 
let 
    val str = valOf (TextIO.inputLine TextIO.stdIn) 
    val i : int = valOf (Int.fromString str) 
in 
    print(Int.toString(i)) 
end 
); 
getNumber(); 
+0

當運行代碼我得到的錯誤:'錯誤:運營商是不是一個函數[tycon不匹配] 操作者:單元 在表達式:(印刷 「請輸入整數的個數:」 讓VAL = VAL = i' – XXIV

+0

我還嘗試添加';'和函數括號的結尾,但是這又給我錯誤'異常選項'再次 – XXIV

+0

是的,';'在函數parens的末尾丟失。 –

相關問題