第三次Java实验报告 | 您所在的位置:网站首页 › 数组倒序算法实验报告 › 第三次Java实验报告 |
Java实验报告
班级 计科二班 学号20188437 姓名 何磊 完成时间 2019/9/22 评分等级 实验三 String类的应用 实验目的 掌握类String类的使用; 学会使用JDK帮助文档; 实验内容1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。) 统计该字符串中字母s出现的次数。 统计该字符串中子串"is"出现的次数。 统计该字符串中单词"is"出现的次数。 实现该字符串的倒序输出。
3.已知字符串"ddejidsEFALDFfnef2357 3ed"。输出字符串里的大写字母数,小写英文字母数,非英文字母数。 实验过程(请自己调整格式) 1.(一)实验代码 package第三次java实验;
public class Thisisatestofjava { public static void main(String[] args) { // TODO Auto-generated method stub int count=0; String s="this is a test of java"; System.out.println((","+s+",").split("s").length-1); //方法一:split函数对字符串s在"s"位置进行拆分,然后通过.length得到拆分后的字符串的个数,减1得到"s"出现的次数 char c[]=s.toCharArray(); for(char e:c){ if(e=='s'){ count++; } } //方法二:用toCharArray函数将字符串s变成字符数组,用foreach循环对字符数组进行遍历,判断并计数 System.out.println((","+s+",").split("is").length-1); //同上方法一 System.out.println((","+s+",").split(" is ").length-1); //同上 for (int i=c.length-1;i>= 0;i--) { System.out.print(c[i]); } //利用字符数组下标递减输出 StringBuffer buffer = new StringBuffer(s); System.out.println("\n"+buffer.reverse()); //定义成一个StringBuffer类,用StringBuffer类中的reverse()方法直接倒序字符串。 } } (二)运行结果 2.(一)实验代码 package 第三次java实验; import java.util.Scanner; public class 加密解密 { public static void main(String[] args) { Scanner input=new Scanner(System.in); String s=input.nextLine(); //输入一个字符串 int length=s.length(); //取得字符串长度 char c[]=s.toCharArray(); //将字符串拆分为单个字符 if(length>2){ //如果字符串长度大于2进入循环 String p=s.substring(length-3); //截取字符串最后3位字符 for(int j=length-4;j>=0;j--){ //将字符串后移3位 c[j+3]=c[j]; } char c1[]=p.toCharArray(); //将截取后的字符串拆分为单个字符 for(int k=0;k |
CopyRight 2018-2019 实验室设备网 版权所有 |