繁体   English   中英

如何编写一个 function 打印两个数字之间的整数

[英]how Write a function that print integers between two numbers

只寻找循环答案。
我想写一个 function n 接受两个数字 arguments x 和 y,并按降序打印 x 和 y 之间的所有整数。
例子:

  • 函数(5.7,5.9)不会打印任何 integer
  • 函数(3.4,6.5)将打印:6,5,4
  • 函数(10,3.5)将打印:10,9,8,7,6,5,4

我试过这个,但它没有给出我正在寻找的东西:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM