My solution without verbosity. It doesn't use any control structure other than function application. It also doesn't use library code to help out. My code is easily extensible to print out the range [a, b]. Just change conts [n / 100]
to conts [(n - a) / (b - a)]
and of course change new Printable (1)
to new Printable (a)
.
To100.java:
class Printable {
private static final Continuation[] conts = {new Next (), new Stop ()};
private final int n;
private final Continuation cont;
Printable (int n) {
this.n = n;
this.cont = conts [n / 100];
}
public void print () {
System.out.println (n);
cont.call (n);
}
}
interface Continuation {
public void call (int n);
}
class Next implements Continuation {
public void call (int n) {
new Printable (n + 1).print ();
}
}
class Stop implements Continuation {
public void call (int n) {
// intentionally empty
}
}
class To100 {
public static void main (String[] args) {
new Printable (1).print ();
}
}
EDIT: Since this question was closed (why???) I'll post my second answer here. It is inspired by Tom Hawtin's notice that the program doesn't have to terminate. Also the question doesn't require that only the numbers 1-100 are printed (or even in order).
To100Again.java:
class To100Again extends Thread {
private static byte n;
public void run () {
System.out.println (n++);
new To100Again ().start ();
System.gc();
}
public static void main (String[] args) {
new To100Again ().start ();
}
}