![](/img/trans.png)
[英]print function of 1 variable and 2 integers leaves gap between the integers
[英]how Write a function that print integers between two numbers
只寻找循环答案。
我想写一个 function n 接受两个数字 arguments x 和 y,并按降序打印 x 和 y 之间的所有整数。
例子:
我试过这个,但它没有给出我正在寻找的东西:
Integers <- function(x, y) {
for(i in y:x) {
print(floor(i))
}
}
Integers(8.7, 8.9)
您可以在进入循环之前添加几个检查
Integers <- function(x, y) {
max <- floor(max(x, y))
min <- ceiling(min(x, y))
if( max < min ) { return() }
for(i in max:min) {
print(floor(i))
}
}
Integers(8.7, 8.9)
Integers(5.7, 5.9)
Integers(3.4, 6.5)
Integers(10, 3.5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.