如何在Groovy中获得函数的输出 static def StartMonth(){ 定义日期=新日期()。格式('MM') def month=date.toInteger() 如果(月份>6){ 月份=第6个月 }else if(month

如何在Groovy中获得函数的输出 static def StartMonth(){ 定义日期=新日期()。格式('MM') def month=date.toInteger() 如果(月份>6){ 月份=第6个月 }else if(month,groovy,Groovy,在groovy中,您可以使用返回值返回一个值(就像您在javascript中所做的那样)。如果没有返回值,则方法返回最后分配的值(在您的示例中是month) 因此,您可以在StartMonth方法中添加返回月 static def StartMonth() { def date = new Date().format('MM') def month = date.toInteger() if (month > 6) { month = m

在groovy中,您可以使用返回值返回一个值(就像您在javascript中所做的那样)。如果没有返回值,则方法返回最后分配的值(在您的示例中是month)

因此,您可以在StartMonth方法中添加
返回月

  static def StartMonth() {
     def date = new Date().format('MM')
     def month = date.toInteger()

     if (month > 6) {
        month = month - 6 
     } else if (month <= 6) {
        month = (month + 12) - 6
     }

  }

  static void main(String[] args) {
      StartMonth();
  } 
} 
在函数中使用
return(XYZ)
语句返回某些内容。其中
XYZ
表达式为变量。
  static void main(String[] args) {
      println StartMonth()
  }