소스 파일명: testIf.rb

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


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

실행> ruby testIf.rb 1.2
1.2 is a positive number.

실행> ruby testIf.rb -1.2
-1.2 is a negative number.

실행> ruby testIf.rb 0
0.0 is zero.



※ 위의 소스 코드는 JRuby로도 (수정 없이) 그대로 실행된다.

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

실행> jruby testIf.rb 3.14
3.14 is a positive number.

실행> jruby testIf.rb -3.14
-3.14 is a negative number.

실행> jruby testIf.rb 0
0.0 is zero.




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

Posted by Scripter
,

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

소스 파일명: TestIfThen.java

  1. public class TestIfThen {
  2.     public static void printUsing() {
  3.         System.out.println("Using: java TestIfThen [number]");
  4.         System.out.println("This determines whether the number is positive or not.");
  5.     }
  6.     // C 언어의 main 함수에 준하는 Java 언어의 main 메소드
  7.     public static void main(String[] args) {
  8.         if (args.length != 1) {
  9.             printUsing();
  10.             System.exit(1);
  11.         }
  12.         ////////////////////////////////////////////////
  13.         // 명령행 인자의 스트링을 가져와서
  14.         // 배정밀도 부동소수점수로 변환하여
  15.         // 변수 val에 저장한다.
  16.         double val = Double.parseDouble(args[0]);
  17.         // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  18.         // 판단하는 if...else... 조건문
  19.         if (val > 0.0)
  20.             System.out.println(val + " is a positive number.");
  21.         else if (val < 0.0)
  22.             System.out.println(val + " is a negative number.");
  23.         else
  24.             System.out.println(val + " is zero.");
  25.     }
  26. }

컴파일> javac -d . TestIfThen.java

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

실행> java TestIfThen 2.1
2.1 is a positive number.

실행> java TestIfThen -2.1
-2.1 is a negative number.

실행> java TestIfThen 0
0.0 is zero.



Groovy를 사용하면 위의 소스를 (컴파일 과정 없이) 그대로 실행시킬 수 있다. 그대신 파일 확장자명을 groovy로 하여 저장하여야 한다. 아래는 파일명을 TestIfThen.groovy로 저장하여 실행시칸 경우이다.


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

실행> groovy TestIfThen.groovy 2.1
2.1 is a positive number.

실행> groovy TestIfThen.groovy -2.1
-2.1 is a negative number.

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




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

Posted by Scripter
,