第五周总结&实验报告三

第五周总结&实验报告三

实验报告

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

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

① 统计该字符串中字母s出现的次数。

package test3;

public class Zifu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        char c[]=str.toCharArray();
        char s='s';
        int count=0;
        for(int j=0;j<=c.length-1;j++)
            if(s==c[j])
                count++;
             
             
    System.out.print("字符s出现的次数:"+count);
      }

}

1580393-20190926213718855-341599889.png

② 统计该字符串中子串“is”出现的次数。

package test3;

public class Zifu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        char c[]=str.toCharArray();
        char s='s',i='i';
        int count=0;
        for(int j=0;j<=c.length-1;j++)
            if(i==c[j]&&s==c[j+1])
                count++;
             
             
    System.out.print(“字串is出现的次数:"+count);
      }

}

1580393-20190926213540604-1383244708.png

③ 统计该字符串中单词“is”出现的次数。

package test3;

public class Zifu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        char c[]=str.toCharArray();
        char s='s',i='i';
        int count=0;
        for(int j=0;j<=c.length-1;j++)
            if(c[j]==' '&&i==c[j+1]&&s==c[j+2]&&c[j+3]==' ')
                count++;
             
             
    System.out.print("单词is出现的次数:"+count);
      }

}

1580393-20190926214047177-955946491.png

④ 实现该字符串的倒序输出。

package test3;

public class Zifu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        String c[]=str.split(" ");
        
        for(int j=c.length-1;j>=0;j--)
            
             System.out.print(c[j]+" ");
      }

}

1580393-20190926214104132-2039999450.png

这个题目其实比较简单,只要掌握了String方法就能做出来。

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

package test4;

import java.util.Scanner;
public class Jiemi {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char a,b,d;
        Scanner scanner = new Scanner(System.in);
        String str=scanner.next();
        char e[]=str.toCharArray();
        char c[]=str.toCharArray();
        String result[]=str.split("");
        for(int x=0;x<result.length;x++) {
            System.out.print(result[x]+" ");
            
        }
        System.out.println(" ");
        a=c[c.length-1];
        b=c[c.length-2];
        d=c[c.length-3];
        for(int i=0;i<c.length-3;i++) {
            e[i+3]=c[i];
            
        }
        e[0]=d;
        e[1]=b;
        e[2]=a;
        for(int i=0;i<c.length;i++)
            System.out.print(e[i]);
        

    }

}

1580393-20190926214227172-906115290.png

这个题目的话我觉得主要考察对字符的运用,以及String类方法的运用,也是比较简单的。

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

package test5;

public class Zifutongji {

    public static void main(String[] args) {
        String str="ddejidsEFALDFfnef2357 3ed";
        int m=0,n=0,k=0;
        char[] c=str.toCharArray();
        for(int i=0;i<str.length();i++)
        {
            if(c[i]>='a'&&c[i]<='z')
            {
                m++;
            }
            else if(c[i]>='A'&&c[i]<='Z')
            {
                n++;
            }
            
            else {
                k++;
            }
        }
        System.out.println("小写字母出现的次数: "+m);
        System.out.println("大写字母出现的次数: "+n);
        System.out.println("其他字符出现的字数: "+k);
        
    }

}

1580393-20190926214437246-664580989.png
这个题目也是考察的字符串类问题,这是一个统计字符的题目,我们以前做过类似的,所以相对来说也没有很大的问题。

课程总结

这周我们学习了子类用extends继承父类的应用,并用super可以从子类中调用父类中的构造方法,
并且学习了方法的覆写,在子类写一个与父类相同的构造方法把父类中的方法覆盖掉,并输出子类方法中的内容
还有抽象类,抽象类必须用abstract声明,抽象类必须被子类继承,,含有抽象方法的类必须是抽象类等等。

转载于:https://www.cnblogs.com/djhxxx/p/11594697.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值