实验三 String类的应用
实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容
1.已知字符串:"this is a test of java".
统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。
我的代码
public class Demo7 {
public static void main(String[] args) {
int count=0;
int count1=0;
int count2=0;
String str1="this is a test of java";
String c[]=str1.split(" ");
for(int i=0;i<str1.length();i++){
char a=str1.charAt(i);
if(a=='s'){
count++; //s出现的次数
}
if(str1.charAt(i)=='i'&&str1.charAt(i+1)=='s'){
count1++; //子串“is”出现的次数
}
}
for(int x=0;x<c.length;x++){
if(c[x].equals("is"))
count2++; //单词“is”出现次数
}
System.out.println("该字符串中字母s出现的次数"+count);
System.out.println("统计该字符串中子串“is”出现的次数"+count1);
System.out.println("统计该字符串中单词“is”出现的次数"+count2);
StringBuffer str = new StringBuffer("this is a test of java");
System.out.println(str.reverse());
}
}
实验结果

2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串

我的代码
import java.util.Scanner;
public class demo8 {
public static void main(String[] args){
System.out.print("字符串:");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
char c[]=str.toCharArray();
String s = "";
for (int i = 0; i < c.length; i++) {
s += (char)((int)c[i]+3);
}
System.out.println(s);
}
}
实验结果

3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。
我的代码
public class Demo9 {
public static void main(String[] args) {
String str1 = "ddejidsEFALDFfnef2357 3ed";
String c[] = str1.split(" ");
int capital = 0;
int lowercase=0;
int count = 0;
for(int i=0;i<str1.length();i++) {
char a = str1.charAt(i);
if(a>='A'&&a<='Z'){
capital++;
}
else if(a>='a'&&a<='z'){
lowercase++;
}
else{
count++;
}
}
System.out.println("大写英文字母数:"+capital);
System.out.println("小写英文字母数:"+lowercase);
System.out.println("非英文字母数:"+count);
}
}
实验结果

总结
继承:在Java类中只允许单一继承,即一个子类只可以继承一个父类,且子类将继承父类的非私有属性和方法,但父类与子类只能一对一,即一个父类下面只有一个子类,但是那个子类可以作为父类进行下一次的继承。
super():
1.super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。
2.super()和this()均需放在构造方法内第一行,因此与this不能同时出现。
浙公网安备 33010602011771号