Java实验报告三

时间:2019-09-25
本文章向大家介绍Java实验报告三,主要包括Java实验报告三使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

① 统计该字符串中字母s出现的次数。

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

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

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

   (1)

package demo;

public class text {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        int sum = str.replaceAll("[^s]", "").length();
        System.out.println(sum);
    }

}

  (2) 

package demo;

public class text {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        String s = "is";
        String[] arr = (","+str.toLowerCase()+",").split(s);
        System.out.println(arr.length - 1);
    }

}

 (3)

(4)

package demo;

public class text {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        StringBuffer s = new StringBuffer(str);
        System.out.println(s.reverse().toString());
        StringBuffer sb2 = new StringBuffer();
        for (int i = str.length()-1; i >=0; i--) {
            s.append(str.charAt(i));
        }
        System.out.println(sb2);
    }
}

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

 代码

package demo;

public class demo1 {

    public static void main(String[] args) {
                String str = "20188492";
                System.out.println(demo1.moveToRight(str, 3));
    }

private static String moveToRight(String str,int position) {
    String str1=str.substring(str.length()-position);
    String str2=str.substring(0, str.length()-position);
    return str1+str2;
}
}

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

 代码

package demo;

public class demo1 {

    public static void main(String[] args) {
                String s = "ddejidsEFALDFfnef2357 3ed";
                int capital = 0;
                int lowercase = 0;
                int numberCount = 0;
         
                for (int i = 0; i < s.length(); i++) {
                    char ch = s.charAt(i);
                    if (ch >= 'a' && ch <= 'z') {
                        lowercase ++;
                    } else if (ch >= 'A' && ch <= 'Z') {
                        capital++;
                    } else if (ch >= '0' && ch <= '9') {
                        numberCount++;
                    }
                }
                
                System.out.println("大写字母" + capital + "个");
                System.out.println("小写字母" + lowercase  + "个");
                System.out.println("数字" + numberCount + "个");

    }

}

原文地址:https://www.cnblogs.com/chenguohhw/p/11544789.html