欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Groovy基础语法 博客分类: Groovy Groovy游戏 

程序员文章站 2024-02-21 20:23:40
...
基础语法
// GDK数值方法
def store = ''
10.times {store += 'x'}
assert store == 'xxxxxxxxxx'
  
store = ''
1.upto 5, {number -> store += number}
assert store == '12345'

store = ''
2.downto(-2) {number -> store += number + ' '}
assert store == '2 1 0 -1 -2 '
  
store = ''
0.step 0.5, 0.1, {number -> store += number + ' '}
assert store == '0 0.1 0.2 0.3 0.4 '
  
// 运算符,运算符实际上是一个方法
// a==b 等价 a.equals(b)
// Groovy的 === 就是 Java的 ==
assert 4>3        //4.compareTo(3) > 0
assert 4<=>3 ==1  //4.compareTo(3)

// 字符串
def name = 'xace'
print "\thello ${name}"
// 原格式输出
println '''
aaaa
bbbb
'''

// 方法 可以省略return,默认参数
def static welcome(defaultStr='hello',name){
  println "${defaultStr} ${name}"
}
welcome('xace')
welcome('fxxk', 'xace')

// 语句特性
for(var in [0,1,10,11,0.5f]) {
  switch(var){
    case 0: print '1'
    case 11..20: print '2'
    case '10': print '3'
    case [1,2,3]: print '4'
    case Float: print '5'
    case {it % 3 == 0}: print '6'
    case ~'[0-9]{3}': print '7'
    default: println ''
  }
}




// 一个小游戏,根据提示输入,返回输入结果
package demo
def printMenu(){
  println ''
  println 'Welcom to Toy Manager View'
  println '=========================='
  println '''
    0.Exit System
    1.Add Toy
    2.Display All Toy
    3.Update Toy
    4.Delete Toy
    5.Display Toy
    6.Delete All Toy
  
      Please Choice
  '''
  new BufferedReader(new InputStreamReader(System.in)).readLine();
}

def choice=printMenu()
while(choice != 0){
  if(choice == '1') println 'add toy'
  else if (choice == '2') println 'display all toy'
  else if (choice == '3') println 'update toy'
  else if (choice == '4') println 'delete toy'
  else if (choice == '5') println 'display toy'
  else if (choice == '6') println 'delete all toy'
  choice = printMenu()
}


相关标签: Groovy 游戏