Random rnd = new Random();
StringBuffer word = null;
switch(rnd.nextInt(3)) {
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);作者: 杨旭 时间: 2012-1-1 19:48
Random rnd = new Random();
StringBuffer word = null;
switch(rnd.nextInt(3)) {
case 0: word = new StringBuffer("P");break;
case 1: word = new StringBuffer("G");break;
default: word = new StringBuffer("M");break;
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
复制代码
作者: 李盈科 时间: 2012-1-1 19:50
你的word = new StringBuffer('P'); 初始化错了!应该写成word = new StringBuffer(“P”); 要双引号!
StringBuffer 的构造方法一共四种
第一个是 public StringBuffer():Constructs a string buffer with no characters in it and an initial capacity of 16 characters.
第二种是public StringBuffer(int capacity):Constructs a string buffer with no characters in it and the specified initial capacity.
第三种是 public StringBuffer(String str):Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16 plus the length of the string argument.
第四种是 public StringBuffer(CharSequence seq):Constructs a string buffer that contains the same characters as the specified CharSequence. The initial capacity of the string buffer is 16 plus the length of the CharSequence argument.
If the length of the specified CharSequence is less than or equal to zero, then an empty buffer of capacity 16 is returned.
作者: 颜宗茂 时间: 2012-1-1 20:30
word = new StringBuffer('P');
word = new StringBuffer('G');
word = new StringBuffer('M');
会调用StringBuffer(int capacity)的构造方法,即将字符转为int类型的变量,表示StringBuffer的大小,所以输出就会那样。作者: 想好了再写 时间: 2012-1-2 18:57