Skip to content

Instantly share code, notes, and snippets.

@entrofi
Last active April 20, 2018 13:28
Show Gist options
  • Save entrofi/63d96ac4197046132912bc9f9d27e0d2 to your computer and use it in GitHub Desktop.
Save entrofi/63d96ac4197046132912bc9f9d27e0d2 to your computer and use it in GitHub Desktop.
Java Puzzlers Un welcome guest
public class Loop {
public static void main(String[] args) {
int[][] tests = {{6, 5, 4, 3, 2, 1}, {1, 2},
{1, 2, 3}, {1, 2, 3, 4}, {1}};
int successCount = 0;
try {
int i = 0;
while (true) {
if (thirdElementIsThree(tests[i++]))
successCount++;
}
} catch (ArrayIndexOutOfBoundsException e) {
// No more tests to process
}
System.out.println(successCount);
}
private static boolean thirdElementIsThree(int[] a) {
return a.length >= 3 & a[2] == 3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment