'프로그래밍/Groovy'에 해당되는 글 34건

  1. 2008.02.19 if...else... 조건문 사용 예제 for Groovy
  2. 2008.02.18 명령행 인자 처리 예제 for Groovy
  3. 2008.02.17 구구단 출력 예제 for Groovy
  4. 2008.02.12 Hello 예제 for Groovy

소스 파일명: 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
,


Groovy 언어에서 args는 예약어이다. 이는 Java 언어에서 main 메소드 정의 구문
         public static void main(String[] args) {
             ..............
         }
에서 매개변수(파라미터) args와 동일한 기능을 같는다. Java 언어라면 이 main 매소드 정의 구문에서 다른 변수로 바꾸어도 되지만, Groovy 언어에서는 args가 에약어로 되어 있으므로 일반 변수 선언시 변수명을 args로 하지 않는 것이 좋다.
 
부분 문자열(스트링)을 구하기 위햐여 범위(range) 연산자 ..<를 사용하였다. 이 연산자는 Ruby 언어의 ...와 동일한 연산자로서 ..와 다른 것은 범위의 마지막 값이 제외된다는 것이다.


소스 파일명: testArguments.groovy

  1. double sum = 0.0
  2. println("Count of arguments: " + args.length)
  3. for (int i = 0; i < args.length; i++) {
  4.     sum += Double.parseDouble(args[i])
  5. }
  6. String strSum = "" + sum
  7. if (strSum.endsWith(".0"))
  8.     strSum = strSum[0..<(strSum.length() - 2)]
  9. println("The sum of arguments is " + strSum)


실행> groovy testArguments.groovy 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> groovy testArguments.groovy 1 2 3 4.5
Count of arguments: 4
The sum of arguments is 10.5





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

Posted by Scripter
,

Groovy 언어의 함수 정의 구문 양식은

       def functionName(parameters) {
             block
     }

이다.

또 Groovy 언어의 전형적인 for 반복문 양식은

       for (varName in Range) {
             block
     }

이다.



소스 파일명: ForTest.groovy
------------------------------[소스 시작]
def printDan(dan) {
  for (i in 1..9) {
    println( "$dan x $i = ${dan*i}" )
  }
}

printDan(2)
------------------------------[소스 끝]

실행> groovy ForTest.groovy
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18





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

Posted by Scripter
,
컨솔에 문자 출력하는 Groovy 구문은

       println "문자열(스트링)"

       print "문자열(스트링)"

이다. println 구문은 개행 문자 "\n" 없이 개행하지만,
print 구문은 개행 문자 "\n"를 추가해야 개행한다.
 
소스 파일명: hello.groovy
------------------------------[소스 시작]
println "Hello, world!"
------------------------------[소스 끝]

실행> groovy hello.groovy
Hello, world!

Posted by Scripter
,