㈡实验内容
⒈ 改错题
下面每个程序都有若干行错误代码(在/*******found******/)。请按要求改正并调试
程序,使之正确运行。必须在原来位置修改,不得增加或删减程序行。
(1)修改错误的代码行,使程序的运行结果 h。
public class SY5_1 {
static char get(String s){ //返回参数字符串s的最后一个字符
/********found********/
int n=s.length; //获得字符串的长度
/********found********/
char c=s.charAt(n);
return c;
}
public static void main (String[] args){
System.out.println(get("asl2h"));
}
}
(2)修改错误的代码行,使程序的运行结果为 true。
public class SY5_2 {
static boolean m(String s){
/********found********/
if(s=="YES") {
return true;
} else {
return false;
}
}
public static void main (String[] args) {
String s=new String("YES");
System.out.println(m(s));
}
}
(3)修改错误的代码行,使程序的运行结果为 HelloJava。
public class SY5_3 {
static String str ="Hello";
//下面方法判断参数s是否为null,若不为null,则将其连接至str的末尾
//并返回连接产生的字符串,否则直接返回字符串str
static String m(String s) {
/********found********/
if(s!=null) str.concat(s);
return str;
}
public static void main (String[] arg) {
System.out.println (m ("Java"));
}
(4)修改错误的代码行,使程序的运行结果为 HelloJava。
public class SY5_4{
static StringBuffer sb = new StringBuffer("Hello");
//下面方法判断参数s是否为null,若不为null,则将其连接至sb的末尾
//并返回连接产生的字符串,否则直接返回字符串sb
static String m(String s) {
if(s!=null) sb.append(s);
/********found********/
return sb;
}
public static void main (String[] arg) {
System.out.println (m ("Java"));
}
(5)修改下面程序,使程序的运行结果为 abcd。
public class Test_Array2 {
public static void main(String[] args){
int [4] ca={'a','b','c','d'};
System.out.println(ca);
}
}
java.docx