实验三 String类的应用
实验目的
掌握类String类的使用;
学会使用JDK帮助文档;实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。
实验内容:
public class 作业1 {
public static void main(String[] args) {
String s="this is a test of java";
int count=0;
char[]a=s.toCharArray();
for(int i=0;i<a.length;i++) {
if(a[i]=='s') {
count++;
}
}
System.out.println("字符出现次数是:"+count);
}
}
知识兔实验结果:
- 实现该字符串的倒序输出。
2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图
实验内容:
public class 作业22 {
public static void main(String[] args) {
String s="this is a test of java";
int count=0;
char[]a=s.toCharArray();
for(int i=0;i<a.length;i++) {
if(a[i]=='i') {
if(a[i+1]=='s') {
count++;
}
}
}
System.out.println("字符is出现的次数是:"+count);
}
}
知识兔实验结果:
3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。
实验内容:
public class 作业3 {
public static void main(String[] args) {
String s="ddejidsEFALDFfnef2357 3ed";
char[]a=s.toCharArray();
int b=0;
int c=0;
int d=0;
for(int i=0;i<a.length;i++) {
if(a[i]>='A'&&a[i]<='Z') {
b++;
}
else if(a[i]>='A'+32&&a[i]<='Z'+32) {
c++;
}
else {
d++;
}
}
System.out.println("大写字母数是:"+b);
System.out.println("小写字母数是:"+c);
System.out.println("非英文字母数是:"+d);
}
}
知识兔实验结果;
4.实验内容:
package shiyan;
public class Test4 {
public static void main(String[] args) {
String str="This is a test of java";
StringBuffer sb = new StringBuffer(str);
System.out.println(sb.reverse().toString());
}
}
知识兔实验结果:
5、
import java.util.*;
public class 作业555 {
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();
}
}
知识兔实验结果:
6、实验内容
import java.util.*;
public class 作业7 {
public static void main(String[] args) {
int a=0,b=0,d=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') {
a++;
}
else if(c[i]>='A'&&c[i]<='Z') {
b++;
}
else
d++;
}
System.out.println("大写字母的个数是:"+b);
System.out.println("小写字母的个数是:"+a);
System.out.println("其它字符个数是:"+d);
}
}
知识兔实验结果:
实验总结:
1、对于实验输出的格式import java.util.*;位置不明确,导致代码总运行出错
2、多态类不了解,通过强制转换子类可以可以转换成父类,而父类不能转换成子类。
3、代码中使用的循环语句不能清晰的读懂,需要调试运行。