询问〜特殊的东西并返回答案的函数

时间:2018-03-11 00:42:11

标签: r string if-statement user-input

我想这样做,但到目前为止我只有:

print("Will you go out with me?")

我希望代码能够正常工作,以便人们可以回答是/否,如果回答是,则会返回一条消息,如时间,地点等。

我问的人是R大师所以对他们来说意味着很多~~(:

会喜欢一些帮助。试着去约会!

1 个答案:

答案 0 :(得分:6)

将以下内容放入文件中,然后让您的朋友来源文件。

r <- readline("Will you go out with me? (respond 'yes' or 'no'): ")
if(grepl("[Yy]es", r)) {
    cat("Date: 3/10/2018", "Time: 7pm", "Place: McDonalds", sep = "\n")
} else {
    cat("Too bad, I'm a catch.")
}

现在你的朋友来源文件和BOOM,日期!

> source('~/.active-rstudio-document')
Will you go out with me?: yes
Date: 3/10/2018
Time: 7pm
Place: McDonalds

...或BOOM,被拒绝了!

> source('~/.active-rstudio-document')
Will you go out with me?: no
Too bad, I'm a catch.