1、计算机二级 JAVA-56及答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:3,分数:100.00)1.本程序统计字母 m在字符串“I am Tom, I am from China.“中出现的次数。 public class exam_37 public static void main(String args) String str=“I am Tom, I am from China.“; char c=m; int i; int sum=0; for(_) _; if(c=temp) _; System.out.println(str+“中“+c+“出
2、现了“+sum+“次“); (分数:30.00)_2.本程序判断一个字符串“Tom“是否在另外一个字符串“I am Tom, I am from China.“中出现。 public class exam_38 public static void main(String args) _; str1=:I am Tom, I am from China.“; str2=“Tom“; int i=_; if(_) System.out.println(“+str2+“is in the string:“+str1+“); else System.out.println(“+str2+“is no
3、t in the string:“+str1+“); (分数:30.00)_3.本程序的功能是将输入的字符串转换为实数,并打印输出。 import java.io.*; public class exam_39 public static void main(String args) InputStreamReader ir; BufferedReader in; ir=new InputStreamReader(_); in=new BufferedReader(ir); double d=0.0; System.out.print(“请输一个实数:“); _ String s=in.rea
4、dLine(); _; catch(Exception e) System.out.println(“你输入的实数是:“+d); (分数:40.00)_计算机二级 JAVA-56答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:3,分数:100.00)1.本程序统计字母 m在字符串“I am Tom, I am from China.“中出现的次数。 public class exam_37 public static void main(String args) String str=“I am Tom, I am from China.“; char c=m
5、; int i; int sum=0; for(_) _; if(c=temp) _; System.out.println(str+“中“+c+“出现了“+sum+“次“); (分数:30.00)_正确答案:(i=0; istr. length(); i+ char temp=str. charAt(i) sum+(或 sum=sum+1或 sum+=1)解析:2.本程序判断一个字符串“Tom“是否在另外一个字符串“I am Tom, I am from China.“中出现。 public class exam_38 public static void main(String args)
6、 _; str1=:I am Tom, I am from China.“; str2=“Tom“; int i=_; if(_) System.out.println(“+str2+“is in the string:“+str1+“); else System.out.println(“+str2+“is not in the string:“+str1+“); (分数:30.00)_正确答案:(String str1, str2 str1. indexOf (str2) i!=-1)解析:3.本程序的功能是将输入的字符串转换为实数,并打印输出。 import java.io.*; pub
7、lic class exam_39 public static void main(String args) InputStreamReader ir; BufferedReader in; ir=new InputStreamReader(_); in=new BufferedReader(ir); double d=0.0; System.out.print(“请输一个实数:“); _ String s=in.readLine(); _; catch(Exception e) System.out.println(“你输入的实数是:“+d); (分数:40.00)_正确答案:(System.in try d=Double.parseDouble (s)解析: