第五周实验报告

1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
•统计该字符串中字母s出现的次数。
•统计该字符串中子串“is”出现的次数。
•统计该字符串中单词“is”出现的次数。
•实现该字符串的倒序输出

2.代码源

package 实验五;

public class zuoy {
    String g = "this is a test of java";
    static zuoy OneString = new zuoy();
    public static void main(String[] args) {
        OneString.numberS();
        OneString.numberIS();
        OneString.numberwordIS();
        OneString.reversal();
    }
    public void numberS() {
        int number = 0;
        for(int i = 0 ; i < g.length(); i++) {
            char c = g.charAt(i);
            if(c=='s') {
                number++;
            }
        }
        System.out.println("S出现次数"+number);
    }
    public void numberIS() {
        int number = 0;
        for(int i = 1 ; i < g.length() ; i++) {
            char c = g.charAt(i-1);
            char c1 = g.charAt(i);
            if(c=='i'&&c1=='s') {
                number++;
            }
        }
        System.out.println("字符IS出现次数"+number);
    }
    public void numberwordIS() {
        int number = 0;
        String s[] = this.g.split(" ");
        for(String s1 : s) {
            if(s1.equalsIgnoreCase("is")) {
                number++;
            }
        }
        System.out.println("单词IS"+number);
    }
    public void reversal() {
        StringBuilder word  = new StringBuilder();
        for(int i = g.length()-1 ; i>=0 ; i--) {
            char c = g.charAt(i);
            word = word.append(g.charAt(i));
        }
        System.out.println("倒序输出 " + word);
    }
}

2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串

代码源

package zuoy2;
import java.util.Scanner;
public class zuoy2 {

    

            public static void main(String[] args) {

                Scanner input=new Scanner(System.in);

                System.out.println("请输入要加密的字串:");

                String str="";

                String str1="";

                int n=0;

                str=input.nextLine();

                n=str.length();

                char temp = 0;

                for(int i=0;i<n;i++)

                {

                    if((str.charAt(i) > 64 && str.charAt(i) < 88)||(str.charAt(i) > 96 && str.charAt(i) < 120))

                        temp=(char) (str.charAt(i) + 3);

                    else if((str.charAt(i) > 87 && str.charAt(i) < 91)||(str.charAt(i) > 119 && str.charAt(i) < 123))

                        temp=(char) (str.charAt(i) - 23);

                    str1+=temp;

                }

                System.out.println("加密后的字串是:
"+str1);

            }

        }

运行结果图

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

package zuoy3;

public class zuoy3 {
    public static void main(String[] args) {
        String str = "ddejidsEFALDFfnef2357 3ed";
        char c[] = str.toCharArray();
        int count1 = 0, count2 = 0, count3 = 0;
        for (int i = 0; i < c.length; i++) {
            int n = (int) c[i]; // 输出ASCII码
            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);

    }

}

运行结果

总结

感觉自己的写的不是很好

上课有点懵逼的感觉,课后自己巩固了一下java的语言顺序,

还是得勤加练习,感觉不用就会忘记的怎样用,上学期的c语言学的都不是很好。

原文地址:https://www.cnblogs.com/Allen15773771785/p/11599081.html