Java实验报告
班级 学号 姓名
完成时间
评分等级
实验三 String类的应用
一、 实验目的
(1) 掌握类String类的使用;
(2) 学会使用JDK帮助文档;
二、 实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
① 统计该字符串中字母s出现的次数。
② 统计该字符串中子串“is”出现的次数。
③ 统计该字符串中单词“is”出现的次数。
④ 实现该字符串的倒序输出。
2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。
![](https://img2018.cnblogs.com/blog/1581808/201909/1581808-20190921133547446-1997666040.png
3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。
三、实验过程
1、第一题
①实验代码
package demo;
public class demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "this is a test of java";
int n = (s.split("s")).length - 1;
System.out.println("s出现的次数" + n);
int t = (s.split("is")).length - 1;
System.out.println("is出现的次数" + t);
int g = (s.split(" is ")).length - 1;
System.out.println(" is 出现的次数" + g);
StringBuffer str = new StringBuffer(s);
str = str.reverse();
System.out.println(str);
}
}
②实验结果
2、第二题
①实验代码
package 实验;
import java.util.Scanner;
public class Word {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(" 加密还是解密 ?");
Scanner input=new Scanner(System.in);
int x;
if(input.nextLine().equals("解密")){
x=0;
}
else {
x=1;
}
String s=input.nextLine();
char[] ch=s.toCharArray();
if(x==1) {
for(int i=0;i<ch.length;i++){
ch[i]=(char)(ch[i]+3);
}
}else {
for(int i=0;i<ch.length;i++){
ch[i]=(char)(ch[i]-3);
}
}
s=s.valueOf(ch);
System.out.println(s);
}
}
②实验结果
3、第三题
①实验代码
package demo;
public class zf {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="ddejidsEFALDFfnef2357 3ed";
char[]ch=s.toCharArray();
int sd=0,sx=0,sf=0;
for(int i=0;i<ch.length;i++) {
if(ch[i]>='A'&&ch[i]<='Z') {
sd++;
}
else if(ch[i]>='a'&&ch[i]<='z'){
sx++;
}
else {
sf++;
}
}
System.out.println(sd);
System.out.println(sx);
System.out.println(sf);
}
}
②实验结果
遇到的小问题百度及翻书解决
ToCharArray( )的用法,将字符串对象中的字符转换为一个字符数组。
四、总结
这周实验主要是对上周学习情况的检测,主要是String类的相关知识;
本周主讲了继承的相关知识
①继承父类与子类的使用;
②子类父类的=中的属性以及方法 重载与复写;
③介绍关键字super,讲调用构造方法;
④char[] up = str.tocharArray();可以将字符串转换成数组,再通过以前学的C语言知识进行简单的转换即可。在本周实验中有使用