11次作业

题目1:编写一个应用程序,统计输入的一个字符串中相同字符的个数,并将统计结果输出。

package 字符串;

import java.util.*;

public class xiangtong {

    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        System.out.println("请输入字符串");
        String s1 = a.next();

           for(int j=0;j<s1.length();j++) {
                int c = 0;
                if(s1.indexOf(s1.substring(j,j+1))==j) {
                    for(int i=0;i<s1.length();i++) {
                        if(s1.regionMatches(i,s1.substring(j,j+1),0, 1)){
                            c++;
                    }
                        }
                    System.out.println(s1.substring(j,j+1)+":"+c);    
                }
            }
    }
}

 

 

题目2:编写程序,输入一个字符串,判断该串中的字母能否组成一个回文串(回文串:一个字符串从前向后读取和从后向前读取都一样)。如:ab<c>c?ba

package 字符串;

import java.util.*;
public class aaaa {
    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        System.out.println("请输入字符串");
        String s1 = a.next();
        int i=0;
        int j=s1.length()-1;
        while(i<j) {
            if(s1.charAt(i)==s1.charAt(j)) {
                
                i++;
                j--;
            }
            else
                System.out.println("不是回文");
        }
    }
}

 

 

 

posted @ 2019-11-19 22:55  l刘磊  阅读(169)  评论(0编辑  收藏  举报