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

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

    package test;
    
    public class F {
    	public static void main(String[] args) {
          String s = "this is a test of java";
        int n = s.indexOf("s",3);
           System.out.println(+n);
        int a = s.indexOf("is");
           System.out.println(+a);
        int b = (s.split(" is ")).length - 1;
            System.out.println("单词is出现的次数" + b);
        StringBuffer r = new StringBuffer ("this is a test of java");
            System.out.println(r.reverse());
    }
    
    }
    


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

    package test;
    
    import java.util.Scanner;
    
    public class Two {
    	public static void main(String[] args) {
    		@SuppressWarnings("resource")
    		Scanner s = new Scanner(System.in);
    		System.out.println("输入字符串:");
    		String r = s.nextLine();
    		char t[] = new char[r.length()];
    		t=r.toCharArray();
    		int i;
    		for (i=0;i<t.length;i++) {
    			t[i]=(char)(t[i]+3);
    		}
    		String c=" ";
    		for (i=0;i<r.length();i++) {
    			c=c+t[i];
    		}
    		System.out.println("改变后的字符串:
    "+c);
    	}
    }
    


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

    package test;
    
    public class Three{
        public static void main(String[] args) {
            String s = "ddejidsEFALDFfnef2357 3ed";
            int min=0,max=0,z,none=0;
            for (z=0;z<s.length();z++) {
                char a=s.charAt(z);
                if (Character.isLowerCase(a)) {
                    min++;
                }
                else if (Character.isUpperCase(a)){
                    max++;
                }
            }
            none=s.length()-min-max;
            System.out.println("大写字母个数:"+max);
            System.out.println("小写字母个数:"+min);
            System.out.println("非英语字母个数:"+none);
        }
    }
    

    总结:
    1):super表示超(父)类的意思,this表示对象本身
    2):使用this调用构造方法,放在首行,不能循环调用。
    2):mian方法中不能使用this和super,super和this不能同时使用。
    对我而言自己写有难度,看书模仿写的。

  • 相关阅读:
    nextcloud环境搭建及部署
    docker容器内访问宿主机,访问不通 错误:Host is unreachable
    记录一下SQL的行行比较
    记录一次nginx平滑升级
    letsencrypt免费SSL证书自动续期
    守护进程因echo挂掉的原因,以及重定向标准输入、标准输出和标准错误
    openresty lua-nginx-module模块中文文档
    nginx localhost的坑
    PHP7 MongoDb的操作类
    tomcat 性能检测
  • 原文地址:https://www.cnblogs.com/FLZ1208/p/11599962.html
Copyright © 2011-2022 走看看