A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

sunwuquan

初级黑马

  • 黑马币:

  • 帖子:

  • 精华:

13黑马币

最佳答案

查看完整内容

import java.util.Scanner; class StringDemo { public static void main(String[] args) { while(true) { show(); } } public static void show() { System.out.print("请输入要判断的字符串:"); Scanner sc = new Scanner(System.in); String line = sc.nextLine(); System.out.println("你输入的是:"+line); int length = line.length(); //先判断字符串的字符个数是奇数还是偶数 //偶数时 if ...

4 个回复

倒序浏览
import java.util.Scanner;

class StringDemo
{
        public static void main(String[] args)
        {
                while(true)
                {
                        show();
                }
        }
        public static void show()
        {
                System.out.print("请输入要判断的字符串:");
                Scanner sc = new Scanner(System.in);
                String line = sc.nextLine();
                System.out.println("你输入的是:"+line);
                int length = line.length();
                //先判断字符串的字符个数是奇数还是偶数
                //偶数时
                if(length%2==0)
                {
                        //截取成前后两个子字符串
                        String s1 = line.substring(0,length/2);
                        String s2 = line.substring( length/2 , length); //注意偶数个和奇数个的不同在此:偶数不加1
                        //将两个字符串转换成字符数组
                        char[] chs1 = s1.toCharArray();
                        char[] chs2 = s2.toCharArray();
                        int chsLength = chs1.length;
                        //统计对比次数和数组长度一致就是符合,否则不符合
                        int count = 0;
                        //遍历数组
                        for(int i = 0 ; i < chsLength; i++)
                        {
                                //对比字符是否相同
                                if(chs1[ i ] !=chs2[ chsLength - i - 1 ])
                                {
                                        System.out.println("这个字符串不符合aba");
                                        break;
                                }
                                //统计对比次数
                                count ++;
                                if(count == chsLength )
                                {
                                        System.out.println("符合aba");
                                }
                        }
                }
                else
                {
                        //截取成前后两个子字符串
                        String s1 = line.substring(0,length/2);
                        String s2 = line.substring( length/2 + 1, length);  //注意偶数个和奇数个的不同在此:奇数加1
                        //将两个字符串转换成字符数组
                        char[] chs1 = s1.toCharArray();
                        char[] chs2 = s2.toCharArray();
                        int chsLength = chs1.length;
                        //统计对比次数和数组长度一致就是符合,否则不符合
                        int count = 0;
                        //遍历数组
                        for(int i = 0 ; i < chsLength; i++)
                        {
                                //对比字符是否相同
                                if(chs1[i] !=chs2[chsLength-i-1])
                                {
                                        System.out.println("这个字符串不符合aba");
                                        break;
                                }
                                //统计对比次数
                                count ++;
                                if(count == chsLength )
                                {
                                        System.out.println("符合aba");
                                }
                        }
                }
        }
}
回复 使用道具 举报
刷日常 谢谢合作  黑马币拿来
回复 使用道具 举报
//                输出一个字符串,怎样判断是否是aba,abcba,abffba,的字符串
                Scanner sca = new Scanner(System.in);
                System.out.println("请输入");
                String str = null;
                if(sca.hasNextLine()) {
                        str = sca.nextLine();
                }
                char[] ch1 = str.toCharArray();
                for (int i = 0; i < ch1.length / 2; i++) {
                        if(ch1[i] != ch1[ch1.length - 1 - i]) {
                                System.out.println("不符合");
                                return;
                        }
                }
                System.out.println("符合");

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马