第五周课程总结&试验报告(三)
发布时间:2020-12-15 07:33:15 所属栏目:Java 来源:网络整理
导读:实验三 String类的应用 实验目的 掌握类String类的使用; 学会使用JDK帮助文档; 实验内容 1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。) 统计该字符串中字母s出现的次数。 实验代码 public class text1 { public
实验三 String类的应用实验目的 public class text1 { public static void main(String[] args) { String str="this is a test of java"; int count=0; char c[]=str.toCharArray(); for(int i=0;i<c.length;i++){ if(c[i]=='s'){ count++; } } System.out.println("s出现了"+count+"次"); } } 实验结果截图 统计该字符串中子串“is”出现的次数。 public class texe2 { public static void main(String[] args) { String str = "this is a test of java"; int count = 0,index = 0; while (true) { int n = str.indexOf("is",index); if (n != -1) { index = n + 1; count++; } else { break; } } System.out.print("is出现了" + count + "次"); } } 实验结果截图 统计该字符串中单词“is”出现的次数。 实验代码 public class text3 { public static void main(String[] args) { int count=0; String str="this is a test of java"; String s[]=str.split(" "); for(int i=0;i<s.length;i++){ if(s[i].equals("is")){ count++; } } System.out.println(count); } } 实验结果截图 实现该字符串的倒序输出。 实验代码 public class text4 { public static void main(String[] args) { String str = "this is a test of java"; char s[] = str.toCharArray(); for (int i=s.length-1;i>=0;i--) { System.out.print(s[i]); } } } 运行结果截图 2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。 实验代码 import java.util.*; public class text1 { public static void main(String[] args) { System.out.print("输入一个字符串:"); var scanner = new Scanner(System.in); String str1 = scanner.nextLine(); System.out.println(str1); char c[] = str1.toCharArray(); int ASCII; char c1; for (int i = 0; i < c.length; i++) { ASCII = c[i] + 3; // System.out.print(ASCII+" "); c1 = (char) ASCII; // System.out.print(c1+" "); String s = String.valueOf(c1); System.out.print(s); } } } 运行结果截图 3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。 实验代码 public class text1 { public static void main(String[] args) { String str = "adhadhahdhahhH 1231"; char c[] = str.toCharArray(); int count1 = 0,count2 = 0,count3 = 0; for (int i = 0; i < c.length; i++) { int n = (int) c[i]; if (65 <= n && n <= 90) { count1++; } else if (97 <= n && n <= 122) { count2++; } else { count3++; } } System.out.println("大写字母数:" + count1); System.out.println("小写字母数:" + count2); System.out.println("非英文字母数:" + count3); } } 运行结果截图 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |