第一题
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
- 统计该字符串中字母s出现的次数。
- 统计该字符串中子串“is”出现的次数。
- 统计该字符串中单词“is”出现的次数。
- 实现该字符串的倒序输出。
?
···
public class?Dome1 {
??? 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.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。
?

?
?实验代码:
import java.util.Scanner;
public class Dome2 {
??? 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”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。
?
实验代码:
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不能同时使用。 对我而言自己写比较有难度,都是看书模仿写的。