Skip to content

Instantly share code, notes, and snippets.

@drei01
Last active December 13, 2015 22:29
Show Gist options
  • Save drei01/4984794 to your computer and use it in GitHub Desktop.
Save drei01/4984794 to your computer and use it in GitHub Desktop.
Answer to the "Pat the Unicorns" puzzle. http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/ # example run javac MagicalLand.java java MagicalLand UNICORN #1: PAT THIS UNICORN ONCE UNICORN #2: PAT THIS UNICORN ONCE END OF PROGRAM
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{
public static boolean pat(){
return true;
}
}
public static class Math{
public static double random(){
return -0.002;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment