首页 > 其他 > 详细

第五周

时间:2019-09-27 17:59:59      阅读:82      评论:0      收藏:0      [点我收藏+]

实验三 String类的应用

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

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

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

实验代码

package test1;

public class test1 {
    private static final String g = null;

    public static void main(String args[]){ 
        //统计字符串字母s的次数
        String str1 = "This is a java test";
        int count1 = 0,count2 = 0,count3 = 0;
        char c[] = str1.toCharArray();
        for(int i = 0;i < c.length;i++){
            if(c[i] == 's'){
                count1 ++;
            }
        }
        System.out.println(count1);
        
        //统计字符串子串is出现的次数
        for(int i = 0;i <c.length;i++){
            if(c[i] == 'i' && c[i+1] == 's'){
                count2 ++;
            }
        }
        System.out.println(count2);
        
        //统计字符串单词is的次数
        String s[] = str1.split(" ");
        for(int i = 0;i < s.length;i++){
            if(s[i].equals("is")){
                count3 ++;
            }
        }
        System.out.println(count3);
        
        //实现该字符串的倒叙输出
        char b[] = str1.toCharArray();
        int a = c.length;
        for(int i = 0;i < c.length;i++){
            b[i] = c[a-1];
            a--;
        }
        System.out.println(b);
        
   }
}

运行结果

技术分享图片

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

实验代码

package test2;

import java .util.Scanner;
public class test2 {
    public static void main(String[] args) {
        System.out.println("加密还是解密?");
        Scanner input=new Scanner(System.in);
        int x;
        if(input.nextLine().equals("加密"))
        {
            x = 1;
        }
        else
        {
            x = 0;
        }
        String str=input.nextLine();
        char[] c=str.toCharArray();
        if(x==1)
        {
            for(int i=0;i<c.length;i++)
            {
                c[i]=(char) (c[i]+3);
            }
        }
        else {
            for(int i=0;i<c.length;i++)
            {
                c[i]=(char)(c[i]-3);
            }
        }
        str=str.valueOf(c);
        System.out.println(str);
    }
}

运行结果

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

实验代码

package test3;

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

运行结果

技术分享图片

  • 本周总结
    这一周老师讲了一些关于面向对象(高级)的内容,关于继承,我感觉上课还是能接受的(大概就是讲了super关键字,final关键字,方法的覆写),但是后来讲的抽象类是真的有点不太懂,这周的作业也不是很难,就第二题有点费头脑,只不过还是做出来了,本周的总结就这么多吧!

第五周

原文:https://www.cnblogs.com/ZhangGuang29/p/11599437.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!