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

实验三 String类的应用

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

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

  • 统计该字符串中字母s出现的次数。
  • 统计该字符串中子串“is”出现的次数。
  • 统计该字符串中单词“is”出现的次数。
  • 实现该字符串的倒序输出。
  • 一:源代码
    package 报告3;
    
    public class Word1 {
      public static void main(String[] args){
          String s="this is a test of java";
          int n=0;
          char[]c=s.toCharArray();
          for(int i=0;i<c.length;i++){
              if(c[i]=='s'){
                  n++;
              }
          }
          System.out.println("字符s出现次数是:"+n);
      }
    }

    二:运行结果截图

  •  一:源代码

    package 报告3;
    
    public class Word1 {
      public static void main(String[] args){
          String s="this is a test of java";
          int n=0;
          char[]c=s.toCharArray();
          for(int i=0;i<c.length;i++){
              if(c[i]=='i'&&c[i+1]=='s'){
                  n++;
              }
          }
          System.out.println("字符串中子串“is”出现次数是:"+n);
      }
    }

    二:运行结果截图

  •  一:源代码

    package 报告3;
    
    public class Word1 {
      public static void main(String[] args){
          String s="this is a test of java";
          int n=0;
          char[]c=s.toCharArray();
          for(int i=0;i<c.length;i++){
              if(c[i]==' '&&c[i+1]=='i'&&c[i+2]=='s'&&c[i+3]==' '){
                  n++;
              }
          }
          System.out.println("字符串中单词“is”出现次数是:"+n);
      }
    }

    二:运行结果截图

  •  一:源代码

    package 报告3;
    
    public class Word1 {
      public static void main(String[] args){
          String s="this is a test of java";
          char[]c=s.toCharArray();
          for(int i=c.length-1;i>=0;i--)
          System.out.println(c[i]);
      }
    }

    二:运行结果截图

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

     

     一:源代码

    package 报告3;
    import java. util.*;
    public class Word2 {
            public static void main(String[] args) {
                Scanner sc=new Scanner(System.in);
                System.out.println("请输入要加密的英文字串");
                String str = sc.nextLine();
                
                char n;
                String str1=new String();
                for(int i=0;i<str.length();i++) {
                    n = str.charAt(i);
                    n = (char)(n+3);  
                    
                    str1+=n;
                }
                System.out.println("加密后的子串是:
    "+str1);
            }
        }

    二:运行结果截图

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

  • 一:源代码
    package 报告3;
    
    public class Word3 {
           public static void main(String args[]){
               String str="ddejidsEFALDFfnef2357 3ed";
               int l=0,m=0,n=0;
               char[] x=str.toCharArray();
               for(int i=0;i<str.length();i++)
               {
                   if(x[i]>='A'&&x[i]<='Z'){
                       l++;
               }
                   else if(x[i]>='c'&&x[i]<='z')
                {
                       m++;
                   }
                   else {
                       n++;
                   }
              }
               System.out.println("大写英文字母出现了"+l+"");
               System.out.println("小写英文字母出现了"+m+"");
               System.out.println("非英文字母出现了"+n+"");
           }
    }

    二:运行结果截图

  • 实验结果总结:这次的java编程题目大部分老师上课都讲过了所以做起来不是很难,第一大题只要有第一小问思路后面3就都

  • 迎刃而解了,后面2题也是帮我巩固这周所学习到的string类的应用,以后我会更加努力的。
原文地址:https://www.cnblogs.com/duweihhw/p/11599816.html