簡體   English   中英

如何使用 ENTER 作為用戶輸入

[英]How to use ENTER as user input

我有一個 for 循環,我希望它只在您在控制台中按 Enter 時轉到下一個“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
 }
}

有什么線索嗎?

一個簡單的解決方案是:

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

這種方法將提示用戶按回車鍵,並在他們這樣做時轉到下一個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) 僅在“輸入”之后顯示,則只需交換兩行printreadline

編輯:根據評論,如果輸入“輸入”以外的內容,您可以中斷

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM