Java learning summary 4

Content Experiments

1. Known string: "this is a test of java" required to do the following requirements :( source code, the results screenshot).

The number of letters in the string s occurs statistics.

The string neutron count the number of times the string "is" appears.

The number of words in the string "is" appears in the statistics.

Achieve reverse the string is output.

package testreport;

public class testreport301 {
    public static void main(String[] args) {
        String str = "this is test of java";
        int count = 0;
        int sum = 0;
        int num = 0;
        String[] v = str.split(" ");
        for (int a = 0; a < str.length(); a++) {
            char c = str.charAt(a);
            if (c == 's') {
                count++;
            }

        }
        for (int n = 0; n < str.length() - 2; n++) {
            String z = str.substring(n, n + 2);
            if (z.equals("is")) {
                sum++;
            }
        }
        for (int m = 0; m < str.length() - 4; m++) {
            String z = str.substring(m, m + 4);
            if (z.equals(" is ")) {
                num++;
            }
        }

        System.out.println("字符串中字母“s”出现的次数:" + count);
        System.out.println("字符串中子串“is”出现的次数:" + sum);
        System.out.println("字符串中单词“is”出现的次数:" + num);
        System.out.print("倒序输出1:");
        for (int j = str.length() - 1; j > 0; j--) {
            char zf = str.charAt(j);
            System.out.print(zf);
        }
        System.out.println();
        System.out.print("倒序输出2:");
        for (int k = 4; k >= 0; k--) {
            System.out.print(v[k]+" ");
        }
    }
}

The results shown in Figure:

2. Write a program, using the following algorithm to encrypt or decrypt the English string input by the user. It requires source code, the results screenshot.

package testreport;
import java.util.Scanner;

public class testreport302 {
    public static void main(String[] args) {
        System.out.println("请输入字符串:");
        Scanner sc=new Scanner(System.in);
        String str1=sc.next();
        char[] c= str1.toCharArray();
        System.out.println("加密后的结果");
        for(char x:c){
            System.out.print((char) (x+3));
        }
    }
}

The results shown in Figure:

3. Given the string "ddejidsEFALDFfnef2357 3ed". The output string in the number of capital letters, lowercase letters count, the number of non-English letters.

package testreport;

import java.util.Scanner;

public class testreport303 {
    public static void main(String[] args)
    {
        Scanner s=new Scanner(System.in);
        String str="ddejidsEFALDFfnef2357 3ed";
        System.out.println("大写字母有:");
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
                System.out.print(str.charAt(i));
            }
        }
        System.out.println();
        System.out.println("小写字母有:");
            for (int j= 0; j < str.length(); j++) {
                if (str.charAt(j) >= 'a' && str.charAt(j) <= 'z') {
                    System.out.print(str.charAt(j));
                }
            }
                System.out.println();
                System.out.println("其他字符有:");
                for (int k= 0; k < str.length(); k++){
                    if(str.charAt(k)<'A'||str.charAt(k)>'z')
                    System.out.print(str.charAt(k));
            }
    }
}

The results shown in Figure:

Guess you like

Origin www.cnblogs.com/94ha-xc/p/11588707.html