Home Java Code Java Code Puzzle #4
Java Code Puzzle #4 PDF Print E-mail
Written by Administrator   
Monday, 08 June 2009 15:48
Ok here is another entertaining one: If you run this program 1000 times, what will it print most(if not all) of the times?
 
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);
 }
}
 
Copyright © 2013 JavaPgmr.net. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
 

Related Links