Created
February 21, 2013 11:45
-
-
Save stetro/5004207 to your computer and use it in GitHub Desktop.
Magical Java Puzzle: “Pat The Unicorns” http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MagicalLand { | |
public static void main(String[] args) { | |
for(int i = 0; i < (Math.random() * 500) + 2; i++) { | |
if(Unicorn.pat()) { | |
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE"); | |
} | |
} | |
for(int i = 0; i < (Math.random() * 500) + 2; i++) { | |
if(Unicorn.pat()) { | |
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE"); | |
} | |
} | |
System.out.println("END OF PROGRAM"); | |
} | |
public static class Unicorn { | |
private static int i = 0; | |
public static boolean pat() { | |
try { | |
throw new Exception(); | |
} catch(Exception e) { | |
for(StackTraceElement l: e.getStackTrace()) { | |
if(l.getMethodName() == "main") { | |
switch(l.getLineNumber()) { | |
case 4: | |
if(i == 0) { | |
i++; | |
return true; | |
} | |
break; | |
case 9: | |
if(i == 1) { | |
i++; | |
return true; | |
} | |
break; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment