第五次学习总结&实验三
2019-09-27 20:51 借过 阅读(125) 评论(0) 编辑 收藏 举报实验三 String类的应用
实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。
1
实验源代码
public class 统计s {
public static void main(String[] args) {
String str = "this is a test of java";
int count=0;
for(int i =0;i<str.length();i++){
char a = str.charAt(i);
if(a == 's'){
count++;
}
}
System.out.println("字母s出现的次数为:"+count);
}
}
实验结果截图
2
实验源代码
public class 统计子串is {
public static void main(String[] args) {
String str = "this is a test of java";
int count=0;
char a[]=str.toCharArray();
for(int i =0;i<str.length();i++){
if(a[i] == 'i'&&a[i+1]=='s'){
count++;
}
}
System.out.println("子串is的数目:"+count);
}
}
实验结果截图
3
实验源代码
public class 统计单词is {
public static void main(String[] args) {
String str = "this is a test of java";
int count=0;
char a[]=str.toCharArray();
for(int i =0;i<str.length();i++){
if(a[i] == 'i'&&a[i+1]=='s'&&a[i-1]==' ') {
count++;
}
}
System.out.println("单词is的次数:"+count);
}
}
实验结果截图
4
实验源代码
package 实验三;
public class 倒序输出 {
public static void main(String[] args) {
String str="this is test a java";
StringBuffer buffer = new StringBuffer(str);
System.out.println(buffer.reverse());
}
}
实验结果截图
总结:本题的话老师在上课的时候差不多都讲完了,自己再打一遍还行,主要是string的几个构造方法的运用,子串is和单词is的区别就是要注意一下中间空格的有无,倒序也就是后面讲到的string buffer的使用。
2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。
实验源代码
import java.util.Scanner;
public class 字串加密 {
static void change() {
System.out.println("请输入字符串:");
Scanner in = new Scanner(System.in);
String str = in.nextLine();
char c[]= str.toCharArray();
char b[] = new char[50] ;
int j=0;
for(int i = c.length-3;i<c.length;i++) {
b[j]=c[i];
j++;
}
for(int z=0;z<c.length-3;z++) {
b[j]=c[z];
j++;
}
System.out.println("加密后的密码为");
System.out.println(b);
}
public static void main(String args[]) {
change();
}
}
实验结果截图
总结:这题感觉有点难,不知道用什么来做,主要的原理是通过改变字符串的位置来加密字串,没弄太懂,还是借鉴了同学的结果,主要是向后移动3位数,不知道怎么解决,还听他们说了ASCII值的操作,还要需要加强。
3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。
实验源代码
package 实验三;
import java.util.Scanner;
public class 字符串字母数 {
public static void main(String[] args) {
int x=0,y=0,z=0;
System.out.println("请输入字符串:");
Scanner in = new Scanner(System.in);
String str = in.nextLine();
char c[]= str.toCharArray();
for(int i=0;i<c.length;i++) {
if(c[i]>='a'&&c[i]<='z') {
x++;
}
else if(c[i]>='A'&&c[i]<='Z') {
y++;
}
else
z++;
}
System.out.println("大写字母的个数:"+y);
System.out.println("小写字母的个数:"+x);
System.out.println("其它字符个数:"+z);
}
}
实验结果截图
总结:这题就正常用toCharArray的string类做就好,用累加的方法得出不同字母的个数,范围限制好大小就好了。
学习总结:
1、static关键字,final关键字,super关键字的学习。
2、本周学习面向对象的知识:继承的基本概念和简单操作。
子类对象的实例化过程