[code]
public class Test {
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');break;
case 2: word = new StringBuffer('G');break;
default:word = new StringBuffer('M');
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
}
}
打印出来是: ain
疯了。。。new StringBuffer()没起作用哈。。。
为什么呢?
因为Char在这些时候都传换成int ,而StringBuffer(int capacity)是接受一个int作为容量,所以当然没用啦。。。。。。
改成字符串把。。。。new StringBuffer("P");
[/code]
public class Test {
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');break;
case 2: word = new StringBuffer('G');break;
default:word = new StringBuffer('M');
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
}
}
打印出来是: ain
疯了。。。new StringBuffer()没起作用哈。。。
为什么呢?
因为Char在这些时候都传换成int ,而StringBuffer(int capacity)是接受一个int作为容量,所以当然没用啦。。。。。。
改成字符串把。。。。new StringBuffer("P");
[/code]