Experiment III to the report and summary

java lab report
third experiment: The String class
A purpose of the experiment
(1) master the use of class String class.
(2) learn to use JDK help documentation.
Second experimental content
1. Known string: "this is a test of java " required to do the following requirements :( source code, the results screenshot).

(1) count the number of letters in the string s appears.
(2) count the number of neutron string the string "is" appears.
(3) count the number of words in the string "is" appears.
(4) achieve reverse the string is output.

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.
https://img2018.cnblogs.com/blog/1581854/201909/1581854-20190920212826600-1853029815.jpg

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

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

(1) count the number of letters in the string s appears.
Experiment Code

package zifu;

public class Letter1 {
    public static void main(String[] args) {
        String str="this is a test of java";
        int count=0;
        for(int i=0;i<str.length();i++) {
            if(str.charAt(i)=='s')
                count++;
        }
        System.out.println("该字符串中字母“s”出现的次数:"+count);
    }
}

Screenshot results

(2) count the number of neutron string the string "is" appears.
Experiment Code

package zifu;

public class Letter2 {
    public static void main(String[] args) {
        String str="this is a test of java";
        int count=0;
        for(int i=0;i<str.length();i++) {
            if(str.charAt(i)=='i'&&str.charAt(i+1)=='s')
                count++;
        }
        System.out.println("该字符串中子串“is”出现的次数:"+count);
    }
}

Screenshot results

(3) count the number of words in the string "is" appears.
Experiment Code

package zifu;

public class Letter3 {
    public static void main(String[] args) {
        String str="this is a test of java";
        String s[]=str.split(" ");
        int count=0;
        for(int i=0;i<s.length;i++) {
            if(s[i].equals("is"))
                count++;
        }
        System.out.println("该字符串中单词“is”出现的次数:"+count);
    }
}

Screenshot results

(4) achieve reverse the string is output.
Experiment Code

package zifu;

public class Letter4 {
    public static void main(String[] args) {
        StringBuffer n=new StringBuffer();
        n.append("this is a test of java");
        String str=n.reverse().toString();
        System.out.println(str);
    }
}

Screenshot results

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.
https://img2018.cnblogs.com/blog/1581854/201909/1581854-20190920212826600-1853029815.jpg

Experiment Code

package zifu;
import java.util.*;
public class Letter6 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        char c[]=s.toCharArray();
        char a[]=new char [c.length];
        int i,j=0;
        if(c.length==1) {
            System.out.println(c[0]);
        }
        else if(c.length==2) {
            System.out.print(c[1]);
            System.out.println(c[0]);
        }
        else
            for(i=c.length-3;i<c.length;i++) {
                a[j]=c[i];
                j++;
            }
            for(i=0;i<c.length-3;i++) {
                a[j]=c[i];
                j++;
            }
            String str=String.valueOf(a);
            System.out.print(a);
    }
}

Screenshot results

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

package zifu;

public class Letter5 {
    public static void main(String[] args) {
        String str="ddejidsEFALDFfnef2357 3ed";
        int a=0, b=0, c=0;
        for(int i=0;i<str.length();i++) {
            if(str.charAt(i)>=65&&str.charAt(i)<=90) {
                a++;
            }
            else if(str.charAt(i)>=97&&str.charAt(i)<=122) {
                b++;
            }
            else
                c++;
        }
        System.out.println("大写字母数:"+a);
        System.out.println("小写英文字母数:"+b);
        System.out.println("非英文字母数:"+c);
    }

}

Screenshot results

Experimental Summary: The first two questions just started writing when using a method of the String of the book, but for String methods of use are not familiar with, there is no use. The first big problem overall is quite simple, mainly to pay attention to small details, such as: when to use the method to be applied; the beginning is often did not add '.'.
The second question just start to feel quite hard, then my classmates and I discussed a moment, looking at the law, I found that was quite simple. But there was a little problem at the time of the second output, while the output of the two characters will become ascii code value of the two characters are added. The last is not resolved, use two statements output.
The third question is not difficult to feel, you can find out by determining the number of each of the letters.
The experimental subject in general without any problems. But we will not use the String class, using a variety of methods are less likely, there is the JDK help files, temporarily not download.

summarize

Guess you like

Origin www.cnblogs.com/hltltp/p/11587470.html