haskell-跳过getLine

haskell-跳过getLine,haskell,getline,Haskell,Getline,嘿-伟大的程序员和Haskeller, 我是哈斯凯尔大学的一年级新生,对一个项目有问题 它归结为以下情况 main :: IO () main = do putStrLn "\nplease give me some input" input1 <- getLine putStrLn "\nplease give me another input" input2 <-getLine putStrLn ("\nyour inputs were "

嘿-伟大的程序员和Haskeller, 我是哈斯凯尔大学的一年级新生,对一个项目有问题 它归结为以下情况

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'
main::IO()
main=do
putStrLn“\n请给我一些输入”

输入1修复:在程序开始时设置
NoBuffering

hSetBuffering stdin NoBuffering
为什么这能解决这个问题?当你不使用NoBuffering时,看看你正在输入什么!您键入,然后
getLine
使用:

first input[enter]
second input[enter]
然后键入,然后
getLine
#2使用:

first input[enter]
second input[enter]
然后键入:

 y[enter]

但是
getChar
只消耗
y
并保留
[enter]
缓冲区,这是您第一次
getLine
调用读取的!为什么键入
[enter]
?因为您必须这样做,仅仅点击“y”不会导致
main
循环,因为终端是行缓冲的。

修复:在程序开始时设置
NoBuffering

hSetBuffering stdin NoBuffering
为什么这能解决这个问题?当你不使用NoBuffering时,看看你正在输入什么!您键入,然后
getLine
使用:

first input[enter]
second input[enter]
然后键入,然后
getLine
#2使用:

first input[enter]
second input[enter]
然后键入:

 y[enter]

但是
getChar
只消耗
y
并保留
[enter]
缓冲区,这是您第一次
getLine
调用读取的!为什么键入
[enter]
?因为您必须这样做,只需点击“y”键并不会导致
main
循环,因为终端是行缓冲的。

感谢这帮了大忙,我是否必须为此导入整个System.IO,还是只能导入您的部分ε/2您需要的是
导入System.IO(hSetBuffering,stdin,BufferMode(NoBuffering))
ahh thx我试图导入NoBuffering而不是BufferMode(NoBuffering)thxε/2tank这帮了大忙,我需要导入整个系统.IO吗?或者我也可以导入部件吗?你的ε/2你需要的是
import System.IO(hSetBuffering,stdin,BufferMode(NoBuffering))
ahh thx我试图导入NoBuffering而不是BufferMode(NoBuffering)thxε/2