数组,异常处理,常用类。
public void change(String str, char ch[]) {
str = "How are you";
ch[1] = 'u';
}
}
运行结果是:( Hi ! Luke )
2. 分析下面的程序,写出运行结果:
public class Exercises5_3 {
public static void main(String args[]) {
String str1 = new String();
String str2 = new String("String 2");
char chars[] = { 'a', ' ', 's', 't', 'r', 'i', 'n', 'g' }; String str3 = new String(chars);
String str4 = new String(chars, 2, 6);
byte bytes[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 };
String str5 = new String(bytes);
StringBuffer strb = new StringBuffer(str3);
System.out.println("The String str1 is " + str1); System.out.println("The String str2 is " + str2); System.out.println("The String str3 is " + str3); System.out.println("The String str4 is " + str4); System.out.println("The String str5 is " + str5); System.out.println("The String strb is " + strb); }
}
运行结果是:( )
The String str1 is
The String str2 is String 2
The String str3 is a string
The String str4 is string
The String str5 is 0123456789
The String strb is a string
五、改错题
1.找出下面代码的错误部分,说明错误类型及原因,并更正。 public int m1 (int number[20]){
number = new int[20];
for(int i=0;i<number.length;i++)
number[i] = number[i-1] + number[i+1];
return number;
}
改正后程序:
public int[] m1(int number[]) {
// number = new int[20];