呼應托馬斯的評論,或許你可以做這樣的事情:
sq<-function(){
d<-readline("give number(0 to 5): ")
if (as.numeric(d)<=5){
dsquare<-(as.numeric(d)^2)
return(dsquare)
}
else{
Message <- paste("Input should be less than or equal to 5.",
"Please enter a new value (0 to 5)", sep = "\n")
message(Message, appendLF=TRUE)
sq()
}
}
這裏有幾個數字的試用:
sq()
# give number(0 to 5): 1.5
# [1] 2.25
sq()
# give number(0 to 5): 6
# Input should be less than or equal to 5.
# Please enter a new value (0 to 5)
# give number(0 to 5): 7
# Input should be less than or equal to 5.
# Please enter a new value (0 to 5)
# give number(0 to 5): 6
# Input should be less than or equal to 5.
# Please enter a new value (0 to 5)
# give number(0 to 5): 2
# # [1] 4
嘗試'Recall'或者只是通過名稱再次調用函數? – Thomas
我試圖找到一個召回()的例子。 Otherway,你的意思是在別的之後再次調用從d <-...到那裏if完成的代碼? – Tony
對於小功能,召回可能沒問題。但是如果我不想從開始就開始這個功能呢?有沒有辦法Recall()的一部分功能?或者,也許我應該把我的功能打破得更小,並從我想重新開始的角度回想起來? – Tony