实验三 String类的应用
实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
public class text { public static void main(String[] args) { int count=0; String str="this is test a java"; for(int i=0;i<str.length();i++) { if(str.substring(i,(i+1)).indexOf("s") != -1)//用substring截取字符串然后用indexOf进行查找 count++; } System.out.println(count); } }
实验结果截图
2.实验代码
public class 英文字串 { public static void main(String[] args) { int count=0; String str="this is test a java"; for(int i=0;i<str.length();i++) { if('i'==str.charAt(i)&&'s'==str.charAt(i+1)) { count++; } } System.out.println("字符is出现的次数为:"+count); } }
实验结果截图
3.实验代码
public class 输出字符串 { public static void main(String[] args) { int count=0; int a=0; String str="this is test a java"; for(int i=0;i<str.length();i++) { if('i'==str.charAt(i)&&'s'==str.charAt(i+1)&&' '==str.charAt(i-1)) { count++; } } System.out.println("单词is出现的次数为:"+count); } }
实验结果截图
4.实验代码
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()); } }
实验结果截图
5.实验代码
import java.util.*; public class text1 { 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 text { 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); } }
实验结果截图
学习总结
this和super的差异:
1.super()和this()均需放在构造方法内第一行。
2.this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。
继承问题:
1.狗类和猫类除了类名不一样,其他的都一样,写起来很浪费时间,也浪费内存.因此,用继承可以提高代码的复用性
其他忘了 难受