本人微信公众号"aeolian"~

回数

 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 

        int num = 0;
        Scanner scanner = new Scanner(System.in);
        num = scanner.nextInt();
        String numString = Integer.toString(num);
        char[] numArr = numString.toCharArray();
        int endIndex = numArr.length-1;
        boolean flag = true; //
        for (int i = 0; i <= endIndex; i++) {
            if (i==endIndex||i>endIndex) {
                break;
            }
            if(numArr[i]!=numArr[endIndex]){
                flag = false;
                break;
            }
            endIndex-=1;
        }
        if (flag) {
            System.out.println("是回数");
        }else {
            System.out.println("不是回数");
        }

源码

点赞

Leave a Reply

Your email address will not be published. Required fields are marked *