如何将输入用作用户输入

发布于 2025-02-09 06:28:51 字数 280 浏览 1 评论 0原文

我有一个循环,我希望它仅在您击中控制台时进入下一个“ i” - 值。所以我写了这篇文章:

for (i in 1:5){
 print(i) 
 UserInput <- readline(prompt = "Press enter to continue.")
 if (UserInput != "ENTER"){ #Obviously "ENTER" is incorrect, but something like that. 
  break
 }
}

有线索吗?

I have a for loop and I want it to only go to the next "i"-value when you hit enter in the console. So I wrote this:

for (i in 1:5){
 print(i) 
 UserInput <- readline(prompt = "Press enter to continue.")
 if (UserInput != "ENTER"){ #Obviously "ENTER" is incorrect, but something like that. 
  break
 }
}

Any clues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2025-02-16 06:28:51

一个简单的解决方案是:

for (i in 1:5){
    readline(prompt=i) }

A simple solution is:

for (i in 1:5){
    readline(prompt=i) }
祁梦 2025-02-16 06:28:51

这种方法将促使用户按Enter按ENTER,并在此操作时转到下一个i

for (i in 1:5){
    print(i) 
    readline(prompt = "Press Enter")
}
# [1] 1
# Press Enter
# [1] 2
# Press Enter
# [1] 3
# Press Enter
# [1] 4
# Press Enter
# [1] 5
# Press Enter

如果您希望第一个结果(1)仅在“ Enter”之后显示,则只需交换这两行,打印readline

编辑:根据评论,如果输入“ Enter”以外的其他内容,您可以中断

for (i in 1:5){
  print(i) 
  input <- readline(prompt = "Press Enter")
  if(input != "") break
}

This approach will prompt the user to press enter, and will go to the next i when they do so:

for (i in 1:5){
    print(i) 
    readline(prompt = "Press Enter")
}
# [1] 1
# Press Enter
# [1] 2
# Press Enter
# [1] 3
# Press Enter
# [1] 4
# Press Enter
# [1] 5
# Press Enter

If you want the first result (1) to show only after an "enter", then just swap the two lines, print and readline.

Edit: based on comment, you can break if something other than an "enter" was input

for (i in 1:5){
  print(i) 
  input <- readline(prompt = "Press Enter")
  if(input != "") break
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文