我正在尝试从输入中读取 Int 并管理来自用户的错误输入。
这段代码可以完成这项工作,但它似乎不起作用......
asknumplayers = do {
putStr "Please the number of other players:\n";
numstr <- getLine;
maybenum <- catch (return (Just (read numstr::Int))) (\(e::SomeException) -> return Nothing);
case maybenum of
Nothing -> asknumplayers;
Just num -> return num;
}
这是主要的:
main = do {
num <- asknumplayers;
putStr $ "Thank you, ready to play against "++ (show num) ++" players?!\n";
}
问题是当字符串打印在主目录时,我仍然得到“ * Exception: Prelude.read: no parse”。是懒惰评估的问题吗?我该如何解决?