第一题

一、题目: 

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

二、源代码:

import java.util.Scanner;//引包
public class Test {//Test类
    public static void main(String[]args){//主方法
        Scanner sc=new Scanner(System.in);//创建对象
        System.out.println("输入字符串:");
        String str=sc.nextLine();//接收字符串
        char[] chars = str.toCharArray();//把字符串转为字符数组
        for(int i = 0; i < str.length(); i++){//for循环遍历
            int num=0;//计数器
            char c = chars[i];
            for(int j = 0; j < str.length();j++) {
                if(c == chars[j]) {//当找到相同字符时计数器加1
                     num++;
                }
            }
            System.out.println(c+ "出现" + num +"");
        }
        
        
    }

}

三、运行结果:

 第二题

一、题目:

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

二、源代码:

import java.util.Scanner;//引包    
public class Huiwen {//创建类
    public static void main(String[] args){//主方法
        Scanner reader=new Scanner(System.in);
        System.out.println("输入一个字符串:");
        String str=reader.nextLine();//接收字符串
        StringBuffer s=new StringBuffer(str);//新建StringBuffer对象
        s.reverse();//将StringBuffer对象中的内容倒置
        String str1=s.toString();
        if(str.equals(str1)){//比较
            System.out.println(str+"是回文串");
        }else{
            System.out.println(str+"不是回文串");
        }
    }
}

三、运行结果: