阅读下列程序,写出程序运行的结果:
public class TestStringBuffer{
public static void main(String args[1){
int n1,n2,n3,n4;
StringBuffer ko1 = new StringBuffer(20);
String str="Hello";
n1=ko1.capacity0;
n2=ko1.length(;
ko1.append(str);
n3=ko1.capacity();
n4=ko1.length(;
ko1.reverse();
ko1.insert(2,'p');
System.out.println("n1="+n1);
System.out.println("n2="+n2);
System.out.println("n3="+n3);
System.out.println("n4="+n4);
System.out.println("ko1="+ko1);
}
程序运行的结果:
(1)______
(2)__.
(3)____
(4)____
(5)
有几处你写错了,应该是:
public class TestStringBuffer {
public static void main(String[] args){
int n1,n2,n3,n4;
StringBuffer ko1 = new StringBuffer(20);
String str="Hello";
n1=ko1.capacity();
n2=ko1.length();
ko1.append(str);
n3=ko1.capacity();
n4=ko1.length();
ko1.reverse();
ko1.insert(2,'p');
System.out.println("n1="+n1);
System.out.println("n2="+n2);
System.out.println("n3="+n3);
System.out.println("n4="+n4);
System.out.println("ko1="+ko1);
}
}
运行结果如下:
n1=20
n2=0
n3=20
n4=5
ko1=olpleH
谢谢!