소스 파일명: testIf.groovy

  1. /*
  2.  *  Filename: testIf.groovy
  3.  *
  4.  *  Purpose:  Example using the conditional control structure syntax
  5.  *                if .... else ...
  6.  *
  7.  *  Execute: groovy testIf.groovy [number]
  8.  */
  9. def printUsing() {
  10.    println("Using: groovy testIf.groovy [number]")
  11.    println("This determines whether the number is positive or not.")
  12. }
  13. if (args.length != 1) {
  14.     printUsing()
  15.     System.exit(1)
  16. }
  17. ////////////////////////////////////////////////
  18. // 명령행 인자의 스트링을 가져와서
  19. // 배정밀도 부동소수점수로 변환하여
  20. // 변수 val에 저장한다.
  21. def val = args[0].toDouble()
  22. // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  23. // 판단하는 if...else... 조건문
  24. if (val > 0.0)
  25.     println("$val is a positive number.")
  26. else if (val < 0.0)
  27.     println("$val is a negative number.")
  28. else
  29.     println("$val is zero.")



실행> groovy testIf.groovy
Using: groovy testIf.groovy [number]
This determines whether the number is positive or not.

실행> groovy testIf.groovy 5
5 is a positive number.

실행> groovy testIf.groovy -5
-5 is a negative number.

실행> groovy testIf.groovy 0
0.0 is zero.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,