加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

第五周课程总结&试验报告(三)

发布时间:2020-12-15 07:33:14 所属栏目:Java 来源:网络整理
导读:实验三 String类的应用 实验目的 掌握类String类的使用; 学会使用JDK帮助文档; 实验内容 1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。) 统计该字符串中字母s出现的次数。 统计该字符串中子串“is”出现的次数。

实验三 String类的应用
实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。
实验代码:
package test;

public class F {
public static void main(String[] args) {
String s = "this is a test of java";
int n = s.indexOf("s",3);
System.out.println(+n);
int a = s.indexOf("is");
System.out.println(+a);
int b = (s.split(" is ")).length - 1;
System.out.println("单词is出现的次数" + b);
StringBuffer r = new StringBuffer ("this is a test of java");
System.out.println(r.reverse());
}

}


2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。
实验代码:
package test;

import java.util.Scanner;

public class Two {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
System.out.println("输入字符串:");
String r = s.nextLine();
char t[] = new char[r.length()];
t=r.toCharArray();
int i;
for (i=0;i<t.length;i++) {
t[i]=(char)(t[i]+3);
}
String c=" ";
for (i=0;i<r.length();i++) {
c=c+t[i];
}
System.out.println("改变后的字符串:n"+c);
}
}


3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数
实验代码:
package test;

public class Three{
public static void main(String[] args) {
String s = "ddejidsEFALDFfnef2357 3ed";
int min=0,max=0,z,none=0;
for (z=0;z<s.length();z++) {
char a=s.charAt(z);
if (Character.isLowerCase(a)) {
min++;
}
else if (Character.isUpperCase(a)){
max++;
}
}
none=s.length()-min-max;
System.out.println("大写字母个数:"+max);
System.out.println("小写字母个数:"+min);
System.out.println("非英语字母个数:"+none);
}
}

总结: 1):super表示超(父)类的意思,this表示对象本身 2):使用this调用构造方法,放在首行,不能循环调用。 2):mian方法中不能使用this和super,super和this不能同时使用。 对我而言自己写有难度,看书模仿写的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读