I have three solutions without a loop in or condition in the Sourcecode
The first one uses the JavaScript Engine to evaluate a string command which is stored in a byte[] at compile-time.
import javax.script.ScriptEngineManager;
import java.nio.charset.StandardCharsets;
public class PrintNumbers
{
public static void main(String... args) throws Exception
{
byte[] iCantSeeALoopHere = new byte[]{102, 111, 114, 32, 40, 105, 32, 61, 32, 49, 48, 59, 32, 105, 32, 60, 61, 32, 54, 48, 59, 32, 105, 43, 43, 41, 32, 123, 32, 112, 114, 105, 110, 116, 40, 105, 41, 59, 32, 125};
new ScriptEngineManager().getEngineByName("JavaScript").eval(new String(iCantSeeALoopHere, StandardCharsets.UTF_8));
}
}
The second one writes a .class-File to the Homedirectory an executes it.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PrintNumbers
{
public static void main(String... args) throws Exception
{
byte[] iCantSeeALoopHere = new byte[]{-54, -2, -70, -66, 0, 0, 0, 52, 0, 31, 10, 0, 5, 0, 17, 9, 0, 18, 0, 19, 10, 0, 20, 0, 21, 7, 0, 22, 7, 0, 23, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 4, 109, 97, 105, 110, 1, 0, 22, 40, 91, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 41, 86, 1, 0, 13, 83, 116, 97, 99, 107, 77, 97, 112, 84, 97, 98, 108, 101, 1, 0, 10, 69, 120, 99, 101, 112, 116, 105, 111, 110, 115, 7, 0, 24, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 17, 80, 114, 105, 110, 116, 78, 117, 109, 98, 101, 114, 115, 46, 106, 97, 118, 97, 12, 0, 6, 0, 7, 7, 0, 25, 12, 0, 26, 0, 27, 7, 0, 28, 12, 0, 29, 0, 30, 1, 0, 12, 80, 114, 105, 110, 116, 78, 117, 109, 98, 101, 114, 115, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 1, 0, 19, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 69, 120, 99, 101, 112, 116, 105, 111, 110, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 121, 115, 116, 101, 109, 1, 0, 3, 111, 117, 116, 1, 0, 21, 76, 106, 97, 118, 97, 47, 105, 111, 47, 80, 114, 105, 110, 116, 83, 116, 114, 101, 97, 109, 59, 1, 0, 19, 106, 97, 118, 97, 47, 105, 111, 47, 80, 114, 105, 110, 116, 83, 116, 114, 101, 97, 109, 1, 0, 7, 112, 114, 105, 110, 116, 108, 110, 1, 0, 4, 40, 73, 41, 86, 0, 33, 0, 4, 0, 5, 0, 0, 0, 0, 0, 2, 0, 1, 0, 6, 0, 7, 0, 1, 0, 8, 0, 0, 0, 29, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 1, 0, 9, 0, 0, 0, 6, 0, 1, 0, 0, 0, 1, 0, -119, 0, 10, 0, 11, 0, 2, 0, 8, 0, 0, 0, 74, 0, 2, 0, 2, 0, 0, 0, 23, 16, 10, 60, 27, 16, 60, -93, 0, 16, -78, 0, 2, 27, -74, 0, 3, -124, 1, 1, -89, -1, -16, -79, 0, 0, 0, 2, 0, 9, 0, 0, 0, 18, 0, 4, 0, 0, 0, 5, 0, 9, 0, 7, 0, 16, 0, 5, 0, 22, 0, 9, 0, 12, 0, 0, 0, 9, 0, 2, -4, 0, 3, 1, -6, 0, 18, 0, 13, 0, 0, 0, 4, 0, 1, 0, 14, 0, 1, 0, 15, 0, 0, 0, 2, 0, 16};
Path javaClassFile = Paths.get(System.getProperty("user.home"), "PrintNumbers.class").toAbsolutePath();
Files.write(javaClassFile, iCantSeeALoopHere);
new ProcessBuilder(
"java",
"-cp",
javaClassFile.getParent().toString(),
javaClassFile.getFileName().toString().replace(".class", "")
).inheritIO().start().waitFor();
Files.delete(javaClassFile);
}
}
The third one uses the getOrDefault
-Method of a Map have something like a condition:
import java.util.Map;
import java.util.HashMap;
import java.util.function.BiConsumer;
public class PrintNumbers
{
private static Map<Integer, BiConsumer<Integer, Integer>> funcMap;
public static void main(String[] args)
{
funcMap = new HashMap<>();
funcMap.put(0, PrintNumbers::doNothing);
printNumbers(10, 60);
}
private static void printNumbers(int curr, int end)
{
System.out.println(curr);
BiConsumer<Integer, Integer> next = funcMap.getOrDefault(end - curr, PrintNumbers::printNumbers);
next.accept(curr + 1, end);
}
private static void doNothing(int a, int b) {}
}