zoukankan      html  css  js  c++  java
  • 第五周课程总结&试验报告(三)

    实验三 String类的应用

    实验目的

    掌握类String类的使用;
    学会使用JDK帮助文档;

    实验内容

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

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

    实验源码
    public class Demo7 {
        public static void main(String[] args) {
            int count=0;
            int count1=0;
            int count2=0;
            String str1="this is a test of java";
            String c[]=str1.split(" ");
            for(int i=0;i<str1.length();i++){
                char a=str1.charAt(i);
                if(a=='s'){
                    count++;              //s出现的次数
                }
                if(str1.charAt(i)=='i'&&str1.charAt(i+1)=='s'){
                count1++;           //子串“is”出现的次数
                }
                }
            for(int x=0;x<c.length;x++){
                if(c[x].equals("is"))
                    count2++;           //单词“is”出现次数
            }
            System.out.println("该字符串中字母s出现的次数"+count);
            System.out.println("统计该字符串中子串“is”出现的次数"+count1);
            System.out.println("统计该字符串中单词“is”出现的次数"+count2);
            
            StringBuffer str = new StringBuffer("this is a test of java");
            System.out.println(str.reverse());
        }
    }
    
    实验结果

    实验过程

    之前使用了非常无脑的方法来统计单词is的出现,后来发现只能判断前面,后面如果是is+其他字符就判断不出来了

       if(str1.charAt(i)==' '&&str1.charAt(i+1)=='i'){
            count2++;           //字串“is”出现的次数
            }
    

    这就是最原始的错误,写代码要考虑很多情况呀,欠缺考虑

    倒序问题参考:
    https://blog.csdn.net/java001122/article/details/80363290

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

    实验源码
    import java.util.Scanner;
    
    public class demo8 {
                public static void main(String[] args){
                    System.out.print("字符串:");  //输入字符串
                    Scanner sc=new Scanner(System.in);
                    String str=sc.nextLine();
                    char c[]=str.toCharArray();   //将字符串变为字符数组
    
                    String s = "";
                    for (int i = 0; i < c.length; i++) {
                        s += (char)((int)c[i]+3);       //后移三位,并且转化为字符进行加密
                    }
                 System.out.println(s);
    
        }
    }
    
    实验结果

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

    实验源码
        public class Demo9 {
        public static void main(String[] args) {
    
            String str1 = "ddejidsEFALDFfnef2357 3ed";
            String c[] = str1.split(" ");
            int capital = 0;
            int lowercase=0;
            int count = 0;
            for(int i=0;i<str1.length();i++) {
                char a = str1.charAt(i);
                if(a>='A'&&a<='Z'){
                    capital++;
                }
                else if(a>='a'&&a<='z'){
                    lowercase++;
                }
                else{
                    count++;
                }
            }
    
            System.out.println("大写英文字母数:"+capital);
            System.out.println("小写英文字母数:"+lowercase);
            System.out.println("非英文字母数:"+count);
        }
    }
    
    实验结果

    实验过程

    大概就是根据ASCII码来写,在编写过程中我竟然把else-if给忘了,一直在想count要怎么计算,太菜了

    本周总结

    1.String类常用操作方法终于会用了,感觉Java上手了
    2.看视频的进度太慢了,要加油赶在老师前面

  • 相关阅读:
    天眼查接口的调用
    EditPlus5.0注册码
    图片转化成base64字符串
    java根据图片的url地址下载图片到本地
    将字符串中的数字转化为文字
    String类型转json 转JSONObject 转 JSONArray 以及 遍历
    三个日期类型比较大小 20181123
    字符与字节的区别
    面向对象五大原则
    Struts/Servlet,action转到jsp后,CSS失效,路径问题(struts2,jsp路径,action路径,action跳转,相对路径,绝对路径)
  • 原文地址:https://www.cnblogs.com/muxixixixi/p/11598323.html
Copyright © 2011-2022 走看看