首页 > 其他 > 详细

第五周

时间:2019-09-27 22:31:12      阅读:84      评论:0      收藏:0      [点我收藏+]

实验三 String类的应用

实验目的

掌握类String类的使用;

学会使用JDK帮助文档;

实验内容

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

统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。

public class zuoye01 {
    
    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);
    }
}

技术分享图片

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

import java.util.Scanner;
public class zuoye02 {
    
    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);
    }

}

技术分享图片

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

public class zuoye03 {

    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);
    }

}

技术分享图片

第五周

原文:https://www.cnblogs.com/xia-unun/p/11600420.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!