Java Reference
In-Depth Information
< Day Day Up >
Puzzle 23: No Pain, No Gain
This program prints a word, using a random number generator to select the first character. Describe
the behavior of the program:
import java.util.Random;
public class Rhymes {
private static Random rnd = new Random();
public static void main(String[] args) {
StringBuffer word = null;
switch(rnd.nextInt(2)) {
case 1: word = new StringBuffer('P');
case 2: word = new StringBuffer('G');
default: word = new StringBuffer('M');
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
}
}
 
 
Search WWH ::




Custom Search