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

    实验三 String类的应用

    实验目的
    掌握类String类的使用;
    学会使用JDK帮助文档;
    实验内容
    1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
    统计该字符串中字母s出现的次数。
    实验代码

    public class text1 {
     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+"次");
                
        }
    }
    

    实验结果截图

    统计该字符串中子串“is”出现的次数。
    实验代码

    public class texe2 {
     public static void main(String[] args) {
            String str = "this is a test of java";
            int count = 0, index = 0;
            while (true) {
                int n = str.indexOf("is", index);
                if (n != -1) {
                    index = n + 1;
                    count++;
                } else {
                    break;
                }
            }
            System.out.print("is出现了" + count + "次");
        }
    }
    

    实验结果截图

    统计该字符串中单词“is”出现的次数。
    实验代码

    public class text3 {
     public static void main(String[] args) {
            int count=0;
            String str="this is a test of java";
            String s[]=str.split(" ");
            for(int i=0;i<s.length;i++){
                if(s[i].equals("is")){
                    count++;        
                }
            }
            System.out.println(count);
        }
    }
    

    实验结果截图

    实现该字符串的倒序输出。
    实验代码

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

    运行结果截图

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

    实验代码

    import java.util.*;
    public class text1 {
     public static void main(String[] args) {
            System.out.print("输入一个字符串:");
            var scanner = new Scanner(System.in);
      String str1 = scanner.nextLine();
            System.out.println(str1);
            char c[] = str1.toCharArray();
            int ASCII;
            char c1;
            for (int i = 0; i < c.length; i++) {
                ASCII = c[i] + 3;
                // System.out.print(ASCII+" ");
                c1 = (char) ASCII;
                // System.out.print(c1+" ");
                String s = String.valueOf(c1);
                System.out.print(s);
            }
        }
    }
    

    运行结果截图

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

    public class text1 {
     public static void main(String[] args) {
            String str = "adhadhahdhahhH 1231";
            char c[] = str.toCharArray();
            int count1 = 0, count2 = 0, count3 = 0;
            for (int i = 0; i < c.length; i++) {
                int n = (int) c[i];
                if (65 <= n && n <= 90) {
                    count1++;
                } else if (97 <= n && n <= 122) {
                    count2++;
                } else {
                    count3++;
                }
            }
            System.out.println("大写字母数:" + count1);
            System.out.println("小写字母数:" + count2);
            System.out.println("非英文字母数:" + count3);
        }
    
    }
    

    运行结果截图

  • 相关阅读:
    TIOBE2017榜单公布_PHP还会是世界上最好的语言吗?
    一个优秀的程序猿应该具备哪些技能?
    7月10日云栖精选夜读:看阿里云窄带高清如何支援优酷 让《楚乔传》更清晰
    如何修复Kindle频繁自动锁屏和解锁
    CentOS 7 配置nginx的service 脚本例子
    Linux系统磁盘分区(逻辑卷LVM)的扩充
    CentOS6.7配置软raid5(模拟故障增加硬盘)
    运行软件显示:缺少packet.dll文件
    《需求工程——软件建模》06
    《需求工程——软件建模》05
  • 原文地址:https://www.cnblogs.com/wsxjydbb/p/11599772.html
Copyright © 2011-2022 走看看