stata 判断输入参数是否正确

capture program drop myfact2
program define myfact2
version 15
capture syntax anything(name=k)
  if _rc{  // 用户忘了输入数字
    dis as error "You must enter an positive integer"
    exit
  }
  if wordcount(`"`k'"')>1{  // 用户输入了多个数字
    dis as error "You can only enter one integer"
    exit
  }
  capture confirm integer number `k' // 用户输入了非整数或文字
  if _rc{
    dis as error "You must enter a positive integer"
    exit
  }
  if `k'<0{  // 用户输入了负数
     dis as error "You must enter an positive integer"
     exit
  }
  local a = 1  //计算
  forvalues i = 1/`k'{
    local a = `a'*`i'
  }
  dis "`k'! = "  `a'
end

 

posted @ 2023-04-19 07:25  myrj  阅读(27)  评论(0编辑  收藏  举报