十一次作业

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

 

package JZ;
import java.util.Scanner;
import java.util.TreeMap;
public class JZ {
    // 统计数字或者字符出现的次数
    public static TreeMap<Character, Integer> Pross(String str) {
        char[] c = str.toCharArray();

        TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();

        for (int x = 0; x < c.length; x++) {
            if (!tm.containsKey(c[x])) {
                tm.put(c[x], 1);
            } else {
                int count = tm.get(c[x]) + 1;
                tm.put(c[x], count);
            }
        }
        return tm;
    }
    
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner re = new Scanner(System.in);
        
        
        System.out.println("请输入一串字符串:");
        String str = re.nextLine();
        TreeMap<Character, Integer> tm = Pross(str);
        System.out.println(tm);
    }
}

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

package JZ;
import java.util.Scanner;
public class hw {
    public static void main(String[] args) {
        Scanner re=new Scanner(System.in);
        String a=re.next();
        boolean c=false;
        int i,j;
        for(i=0,j=a.length()-1;i<j;i++,j--){
            if(a.charAt(i)!=a.charAt(j)){
                System.out.println("不是回文数");
                break;
            }
        }
        if(i>=j){
            System.out.println("是回文数");
        }
    }
}

 

 

 

 

posted @ 2019-11-20 17:37  沐之晴  阅读(143)  评论(0编辑  收藏  举报