第五周课堂总结

  • 实验目的
  • 掌握类String类的使用;
  • 学会使用JDK帮助文档;
  • 实验内容

1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)

  • 统计该字符串中字母s出现的次数。
  • 统计该字符串中子串“is”出现的次数。
  • 统计该字符串中单词“is”出现的次数。
  • 实现该字符串的倒序输出。

源代码:

1:

package test;

public class Test1 {
    public static void main(String args[]) {
        String str="this is a test of java";
        int count = 0;
        char c[]=str.toCharArray();
        for(int i=0;i<c.length;i++){
            if(c[i]=='s'){ 
                count++;
            }           
         }
        System.out.println("字母s出现了"+count+"次");
            
    }

}

截屏:

2:

package test;

public class Test2 {
    public static void main(String args[]) {
        String str="this is a test of java";
        int count = 0;
        char c[]=str.toCharArray();
        for(int i=0;i<c.length;i++){
            if(c[i]=='i'&&c[i+1]=='s'){ 
                count++;
            }           
         }
        System.out.println("子串is出现了"+count+"次");
            
    }

}

截屏:

3:

package test;

public class Test3 {
    public static void main(String args[]) {
        String str="this is a test of java";
        int count = 0;
        char c[]=str.toCharArray();
        for(int i=0;i<c.length;i++){
            if(c[i]==' '&&c[i+1]=='i'&&c[i+2]=='s'&&c[i+3]==' '){ 
                count++;
            }           
         }
        System.out.println("单词is出现了"+count+"次");
            
    }


}

截屏

4:

package test;

public class Test4 {
    public static void main(String args[]) {
        String str="this is a test of java";
        char c[]=str.toCharArray();
        for(int i=c.length-1;i>=0;i--){                           
        System.out.println(c[i]);
        }
            
    }
 
}

截屏

2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。

 

 3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。

源代码:

package test;

public class Test5 {
       public static void main(String args[]) {
           String str = "ddejidsEFALDFfnef2357 3ed";
           int d = 0,x = 0,f = 0;
           char[] c=str.toCharArray();
           for(int i = 0;i<str.length();i++) {
               if(c[i]>='A'&&c[i]<='Z') {
                   d++;
               }
               else if(c[i]>='a'&&c[i]<='z') {
                   x++;
               }
               else {
                   f++;
               }
           }
           System.out.println("大写英文字母出现了"+d+"次");
           System.out.println("小写英文字母出现了"+x+"次");
           System.out.println("非英文字母出现了"+f+"次");
           
       }
}

截屏:

学习总结

1、学习了This跟super的区别,并且this与super不能齐用。

2、学习了子类父类关系和继承的注意事项。

3、学习了父类与子类之间的转换关系,子类可以自动转化为父类,而父类只能强制转化为子类。

原文地址:https://www.cnblogs.com/Bowen----/p/11597934.html