main :: IO ()
main = do
putStrLn "\nplease give me some input"
input1 <- getLine
putStrLn "\nplease give me another input"
input2 <-getLine
putStrLn ("\nyour inputs were "++show(input1)++" and "++ show(input2)")
putStrLn "restart ?? yY or nN"
c <- getChar
restart c
where
restart c
|elem c "yY" = do
main
|elem c "nN" = putStrLn "\nExample Over"
|otherwise = do
putStrLn "\nyou must type one of Yy to confirm or nN to abort"
c'<- getChar
restart c'
除了第一次执行主
之外的其他任何内容input1 <- getLine
跳过,我找不到理由,如下所示
input2 <- getLine
按预期执行,我愿意接受任何建议和帮助 提前谢谢ε/ 2
答案 0 :(得分:5)
修复:在程序开头设置NoBuffering
:
hSetBuffering stdin NoBuffering
为什么这会解决问题?当你不使用NoBuffering时,看看你正在输入什么!您输入,getLine
消费:
first input[enter]
然后键入,getLine
#2消耗:
second input[enter]
然后输入:
y[enter]
但是getChar
只消耗了y
并留下了[enter]
缓冲,这是您的第一个getLine
来电读取的!你为什么键入[enter]
?因为你必须这样做,只是点击'y'不会导致main
循环,因为终端是行缓冲的。