• 第五周课程总结&试验报告(三)


    实验三 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);
        }
    
    }
    

    运行结果截图

  • 相关阅读:
    nginx安装配置: configure命令
    nginx最简安装
    进程上下文切换
    九卷读书:《操作系统设计与实现》读书笔记
    计算机存储器的层次结构
    线程,进程和并发
    理解Flight框架核心
    Ubuntu16.04安装QQ机器人
    微信支付解决方案
    springboot +nginx +freemarker 模板的简单集成
  • 原文地址:https://www.cnblogs.com/wsxjydbb/p/11599772.html
Copyright © 2020-2023  润新知