zoukankan      html  css  js  c++  java
  • 判断回文字符串的方法

    import java.util.Scanner;
    //判断回文字符串的方法
    //将字符串倒置后逐一比较
    public class HuiWenTest { public static void main(String[] args) { String string = ""; System.out.println("请输入一个字符串:"); Scanner input = new Scanner(System.in); string = input.next(); StringBuffer sb = new StringBuffer(string); sb.reverse();// 将string中的字符串倒置 int count = 0; for (int i = 0; i < string.length(); i++) { if (string.charAt(i) == sb.charAt(i)) { count++; } } if (count == string.length()) { System.out.println("返回的是一个回文字符串"); } else { System.out.println("此字符串不是一个回文字符串"); } } }
    public class HuiWenTest2 {
        //将字符串倒置后创建新字符串直接比较
        public static void main(String[] args) {
            System.out.println("请输入一个字符串");
            Scanner input = new Scanner(System.in);
            String str = input.next();
            StringBuilder sb=new StringBuilder(str);
            sb.reverse();//将str倒置的方法
            String newStr=new String(sb);
            if(str.equals(newStr)){
                System.out.println(str+"是回文字符串");
            }else{
                System.out.println(str+"不是回文字符串");
            }
        }
    }
    public class HuiWenTest3 {
        //使用截取字符串的方式比较
        public static void main(String[] args) {
            System.out.println("请输入一个字符串");
            Scanner input = new Scanner(System.in);
            String str = input.next();
            int count = 0;
            for (int i = 0; i < str.length() / 2; i++) {
            if ((str.substring(i, i + 1)).equals(str.substring(str.length() - 1- i, str.length() - i))) {
                    count++;
                }
            }
            if (count == str.length() / 2) {
                System.out.println("是回文字符串");
            }else{
                System.out.println("不是回文字符串");
            }
        }
    }
    public class HuiWenTest4{
            //判断回文数字(判断纯数字)
        public static void main(String[] args) {
            int n;
            System.out.println("请输入一个整数:"); 
            // 如果结果为回文数,跳出循环
            while (true) {
                Scanner InpuNum = new Scanner(System.in);
                n = InpuNum.nextInt();
                if (isHuiWen(n)) {
                    System.out.println(n + "是回文数!");
                    break;
                } else {
                    System.out.println(n + "不是回文数!");
                }
            }
        }
    
    
        // 判断的数字是否是回文数
        public static boolean isHuiWen(int n) {
            int m = reverse(n);
            if (m == n) {
                return true;
            } else {
                return false;
            }
        }
    
    
        // 将输入的数字进行倒置,以便进行判断是否是回文数
        public static int reverse(int n) {
            int temp = 0;// 临时变量
            int j = 0;// 倒置后的数字
            temp = n;// 将输入的数字赋值给临时变量
            while (temp != 0) {
                j = j * 10 + temp % 10;//依次获取最后一位
                temp /= 10;//依次获取最后一位后在将最后一位数字去掉,在继续while循环
            }
            return j;
        }
    }
  • 相关阅读:
    [LeetCode] 148. Sort List 解题思路
    [LeetCode] 21. Merge Two Sorted Lists 解题思路
    [LeetCode] 160. Intersection of Two Linked Lists 解题思路
    [LeetCode] 203. Remove Linked List Elements 解题思路
    是否是最美的6年
    Apache的Order Allow,Deny 详解
    apache 2.4 访问权限配置
    apache如何设置http自动跳转到https
    linux ssh_config和sshd_config配置文件
    mysql命令查询表的个数
  • 原文地址:https://www.cnblogs.com/fifiyong/p/6405310.html
Copyright © 2011-2022 走看看