Java Experiment 3 and the fifth weekly summary

1. Known string: "this is a test of java " required to do the following requirements :( source code, the results screenshot)..
• count the number of letters in the string s appears.
• Statistical neutron string the string "is" number of occurrences.
• count the number of words in the string "is" appears.
• achieve reverse output of the string.

public class Test5 {
    
    String s = "this is a test of java";
    static Test5 OneString = new Test5();
    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 < s.length(); i++) {
            char c = s.charAt(i);
            if(c=='s') {
                number++;
            }
        }
        System.out.println("S出现次数"+number);
    }
    public void numberIS() {
        int number = 0;
        for(int i = 1 ; i < s.length() ; i++) {
            char c = s.charAt(i-1);
            char c1 = s.charAt(i);
            if(c=='i'&&c1=='s') {
                number++;
            }
        }
        System.out.println("字符IS出现次数"+number);
    }
    public void numberwordIS() {
        int number = 0;
        String s[] = this.s.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 = s.length()-1 ; i>=0 ; i--) {
            char c = s.charAt(i);
            word = word.append(s.charAt(i));
        }
        System.out.println("倒序输出 " + word);
    }
}

This question I did not convert a string to an array of re-operation. In judging my character is not initially think of how to make the loop detection two characters, and finally checked the Internet and found that you can build two char-type variables for testing.
When looking for a word, although separated by a space, but I was not initially thought of a good way to contrast the word is not is, finally asked a friend, get a new method ·.

2. Write a program, using the following algorithm to encrypt or decrypt the English string input by the user.

import java.util.Scanner;
public class Test52 {
    
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        StringBuilder s = new StringBuilder();
        String s2 = " ";
        String s1 = scanner.next();
        for(int i = 0 ; i < s1.length() ; i++) {
            int c = s1.charAt(i);
            if(c>=65 && c<=90) {
                if(c==88) {
                    c = 65;
                }else if(c==89) {
                    c = 66;
                }else if(c==90) {
                    c = 67;
                }else {
                    c+=3;
                }
                s2 = String.valueOf(s.append((char)c));
            }else
            if(c>=97 && c<=122) {
                if(c==120) {
                    c = 97;
                }else if(c==121) {
                    c = 98;
                }else if(c==122) {
                    c = 99;
                }else {
                    c+=3;
                }
                s2 = String.valueOf(s.append((char)c));
            }
        }
        System.out.println("加密前 "+s1);
        System.out.println("加密后 "+s);
    }

}

At first I did not understand the meaning of this question, I thought it was just a string moved back three, then found that even encrypted, so a start can not understand.
After my friend some advice, I think of it C language teacher once said of encryption, the last attempt in this respect the next.

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

public class Test53 {

    static String s = "ddejjdsEFALDFfnef2357 3ed";
    public static void main(String[] args) {
        int Word = 0;
        int word = 0;
        int other = 0;
        for(int i = 0;i < s.length();i++) {
            char c = s.charAt(i);
            if(c>='A' && c<='Z') {
                Word++;
            }else 
            if(c>='a' &&c<='z') {
                word++;
            }else {
                other++;
            }
        }
        System.out.print("大写字母: "+Word+",小写字母 "+word+",其他 "+other);
    }

}

The subject compared to the first two questions is relatively simple, there is nothing wrong.

Fifth week study concluded
this week finally through their own self-study learned something:
since last week in the classroom teacher to explain the string class knowledge, so this week in homework, mainly around the string and learning:
1 .char [] up = str.tocharArray () ; can be converted into a string array, then converted to a simple C language knowledge of previously learned.
2.string s [] = this.s.split ( " "); can be cut, I think that when looking for the word is useful.
3.stringBuilder and stringBuffer string variable. attend and method of the insert method stringBuilder has a good role in adding character or string.
4.valueOf can be converted into other types of variables string type variables.
This is just some of the self-study summary, if later found to have incomplete, and then add

Guess you like

Origin www.cnblogs.com/arthur-w/p/11593596.html