Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的闰年某些输出是错误的_R - Fatal编程技术网

R中的闰年某些输出是错误的

R中的闰年某些输出是错误的,r,R,在R中找出闰年 year <- 1990 if((year %% 4) == 0){ if((year %% 100) == 0){ if((year %% 400) == 0){ print(paste(year,"is a leap year")) } else{ print(paste(year,"is a leap year")) } } else{ print(paste(year,"is not a leap year")) } } year

在R中找出闰年

year <- 1990
if((year %% 4) == 0){
  if((year %% 100) == 0){
   if((year %% 400) == 0){
   print(paste(year,"is a leap year"))
}
 else{
  print(paste(year,"is a leap year"))
 }
}
 else{
   print(paste(year,"is not a leap year"))
 }
}

year我认为您尝试的逻辑有点不正确。此外,对于第一个
if
,您还需要在末尾添加另一个
else

year <- 1990

if((year %% 4) == 0) {
  if((year %% 100) == 0) {
    if((year %% 400) == 0) {
      print(paste(year,"is a leap year"))
    } else {
      print(paste(year,"is not a leap year"))
    }
  } else {
     print(paste(year,"is a leap year"))
  }
} else {
  print(paste(year,"is not a leap year"))
}
特别是,函数中的闰年由
(year%%4==0)和((year%%100!=0)|(year%%400==0))

我认为您尝试时的逻辑有点不正确。此外,对于第一个
if
,您还需要在末尾添加另一个
else

year <- 1990

if((year %% 4) == 0) {
  if((year %% 100) == 0) {
    if((year %% 400) == 0) {
      print(paste(year,"is a leap year"))
    } else {
      print(paste(year,"is not a leap year"))
    }
  } else {
     print(paste(year,"is a leap year"))
  }
} else {
  print(paste(year,"is not a leap year"))
}
特别是,函数中的闰年由
(year%%4==0)和((year%%100!=0)|(year%%400==0))

因为1990年不是闰年。这也错误地给出17001800和1900是闰年,而实际上它们不是,因为1990年不是闰年。这也错误地给出17001800和1900是闰年,而实际上它们不是