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

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

    package zy123;
    
    public class Test {
    
    	 public static void main(String[] args) { 
             String str="This is a test of java";
             int count=0;
             for(int i=0;i<str.length();i++) {
            	if('s'==str.charAt(i)) //查找s
            		count++;
            	}
            	System.out.println("字母s出现的次数:"+count);
             }
    }
    

    实验截图:

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

    package zy123;
    
    public class Test {
    
    	public static void main(String[] args){
            String str="this is a test of java";
                  int count=0;
            for(int i=0;i<str.length();i++) {
                if(str.charAt(i)=='i'&&str.charAt(i+1)=='s') 
                    count++;
                             
            }
            System.out.println("字符串中子串“is”出现的次数:"+count);
        }
    }
    

    实验截图:

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

    package zy123;
    
    public class Test {
    
    	public static void main(String[] args) {
    		String str="This is a test of java";
                String s[]=str.split(" ");
              int count=0;
              
            for(int i=0;i<s.length;i++) {
                  if(s[i].equals("is"))
                    count++;
            }
            System.out.println("字符串中单词“is”出现的次数:"+count);
        }
    }
    

    实验截图:

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

    package zy123;
    
    public class Test {
    
    	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.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。

    public class demo5 {
        public static void main(String[] args) {
            Scanner sc =new Scanner(System.in);
            String str1 = sc.nextLine();
            char c[] = str1.toCharArray();
            char a[] = new char[str1.length()];
            int i,j=0;
            if(str1.length()==1) {
                System.out.println(c);
            }
            else if(str1.length()==2) {
                System.out.print(c[1]);
                System.out.print(c[0]);
            }
            else {
                for(i = c.length-3;i<c.length;i++) {
                    a[j] = c[i];
                    j++;
                }
                for(i=0;i<c.length-3;i++) {
                    a[j]=c[i];
                    j++;
                }
            }
            System.out.println(a);
        }
    }
    
    

    代码出处:https://www.cnblogs.com/leisidiya/p/11580804.html 自己还需要多理解

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

    package zy123;
    
    public class Test {
    
    	public static void main(String[] args) 
        {
            String str="ddejidsEFALDFfnef2357 3ed";
            int one=0, two=0, three=0;
            for(int i=0;i<str.length();i++)
            {
                if(str.charAt(i)>='A'&&str.charAt(i)<='Z')
                {
                    one++;
                }
                else if(str.charAt(i)>='a'&&str.charAt(i)<='z')
                {
                    two++;
                }
                else
                  three++;
            }              
              System.out.println("大写字母数:"+one);
              System.out.println("小写英文字母数:"+two);
              System.out.println("非英文字母数:"+three);
        }
    
    }
    

    实验截图:刚开始不太懂 后来才明白要用ASCII码值来判断
    总结:这周学习了了继承、多态、覆写、重载、final和抽象类的内容也学习了String类函数的定义和使用
    运用来找字符串的问题 还有很多需要学的地方。

  • 相关阅读:
    Javascript 生成全局唯一标识符 (GUID,UUID)
    nginx 与location语法详解
    nginx的安装与使用
    Linux上python3的安装和使用
    Linux上Redis安装和简单操作
    MySQL + centos +主从复制
    web服务基础
    linux系统基础优化及高级操作命令
    vim编辑
    Linux的基本命令
  • 原文地址:https://www.cnblogs.com/clearlove1215/p/11600912.html
Copyright © 2011-2022 走看看