소스 파일명: testIf.groovy
- /*
- * Filename: testIf.groovy
- *
- * Purpose: Example using the conditional control structure syntax
- * if .... else ...
- *
- * Execute: groovy testIf.groovy [number]
- */
- def printUsing() {
- println("Using: groovy testIf.groovy [number]")
- println("This determines whether the number is positive or not.")
- }
- if (args.length != 1) {
- printUsing()
- System.exit(1)
- }
- ////////////////////////////////////////////////
- // 명령행 인자의 스트링을 가져와서
- // 배정밀도 부동소수점수로 변환하여
- // 변수 val에 저장한다.
- def val = args[0].toDouble()
- // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
- // 판단하는 if...else... 조건문
- if (val > 0.0)
- println("$val is a positive number.")
- else if (val < 0.0)
- println("$val is a negative number.")
- else
- 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.
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
'프로그래밍 > Groovy' 카테고리의 다른 글
Groovy 언어의 특징을 잘 나타내는 몇 가지 예제들 (0) | 2008.02.21 |
---|---|
(최대공약수 구하기) while... 반복문 예제 for Groovy (0) | 2008.02.20 |
명령행 인자 처리 예제 for Groovy (0) | 2008.02.18 |
구구단 출력 예제 for Groovy (0) | 2008.02.17 |
Hello 예제 for Groovy (0) | 2008.02.12 |