2019秋JAVA第三周课程总结及实验报告(三)
实验内容
一、已知字符串: "this is a test of java" 按要求执行以下操作: ( 要求源代码、结果截图。)
1统计该字符串中字母s出现的次数。,
2统计该字符串中子串“is”出现的次数。
3统计该字符串中单词“is”出现的次数。
4实现该字符串的倒序输出。
二、请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。
三、已知字符串“ddejidsEFALDFfnef2357 3ed” 。输出字符串里的大写字母数,小写英文字母数,非英文字母数。
实验代码
1.
1 public static void main(String[] args) 2 { 3 String s = "this is a test of java"; 4 char [] c = s.toCharArray(); 5 int s_count = 0; 6 int is1_count = 0; 7 int is2_count = 0; 8 9 for (char x:c) { 10 if(x=='s') 11 { 12 s_count++; 13 } 14 } 15 16 char tmp = c[0]; 17 for (int x=1; x<c.length; x++) { 18 tmp = c[x-1]; 19 if(tmp=='i' && c[x]=='s'){ 20 is1_count++; 21 } 22 23 } 24 25 String[] sa = s.split(" "); 26 for (String f:sa) { 27 if(f.equals("is")) 28 { 29 is2_count++; 30 } 31 32 } 33 34 35 System.out.println(s_count); 36 System.out.println(is1_count); 37 System.out.println(is2_count); 38 39 40 }
2.
1 public void Encode(String s) { 2 private char [] c; 3 c = s.toCharArray(); 4 for (char x:c) { 5 System.out.print((char)(x+3)); 6 } 7 }
3.
1 public void Alpha(String s) 2 { 3 char [] c; 4 int h_count = 0; 5 int l_count = 0; 6 int c_count = 0; 7 c = s.toCharArray(); 8 for (char x:c) { 9 if(x>60 && x<90){ 10 h_count++; 11 } 12 else if(x>97 && x<122){ 13 l_count++; 14 } 15 else{ 16 c_count++; 17 } 18 } 19 20 System.out.println(h_count); 21 System.out.println(l_count); 22 System.out.println(c_count); 23 }
总结
第一题的is判断多花了几分钟调试,其他都使用字符数组进行操作,以后应该要好好看文档用String类方法操作。

浙公网安备 33010602011771号