2016-02-29 43 views
0

我想制作一款游戏,并且我想问问玩家一个问题。如果他们的回答是肯定的,则会发生某种事情或者没有其他事情发生使用实例进行输入检查?

class Status a where 
    stat :: a -> String 

instance Status Char where 
    stat _ = "Would you like to drink a potion? YES or NO?" 

这样做会检查所有的字符类型,但我不能使用字符串。我可以用不同的方式做到这一点吗?

+3

为什么这个问题需要类型类/实例?那么如果答案==“是”,那么......否则......“呢? – chi

+0

我需要写这至少20次,所以想知道我是否可以用实例代替它,并缩短我的代码。 – user35053

+2

@ user35053只是将它包装在一个可以调用20次的函数中? – mb21

回答

1

我觉得这是你想要

main = do 
    putStrLn "type yes or no" 
    response <- getLine 
    if response == "yes" 
     then putStrLn "You typed yes" 
     else putStrLn "You didn't type yes" 
0

这听起来像你想是这样的,而不是:

import Data.Char 

getStatus :: String -> IO Bool 
getStatus question = do 
    putStrLn (question ++ " YES or NO?") 
    response <- getLine 
    return (toLower response == "y") 

main = do 
    res1 <- getStatus "question1?" 
    res2 <- getStatus "question2?" 
    . 
    . 
    . 
    res20 <- getStatus "question20?" 
    doSomething [res1, ..., res20] 
相关问题