Java Reference
In-Depth Information
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
}
}
Although this program fixes the bugs, it is overly verbose. Here is a more elegant version:
import java.util.Random;
public class Rhymes {
private static Random rnd = new Random();
public static void main(String args[]) {
System.out.println("PGM".charAt(rnd.nextInt(3)) + "ain");
}
}
Better still is the following version. Although slightly longer, it is more general. It does not depend
on the fact that the possible outputs differ only in their first characters:
import java.util.Random;
public class Rhymes {
public static void main(String args[]) {
String a[] = {"Main", "Pain", "Gain"};
 
 
Search WWH ::




Custom Search