⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.1checkpalindrome.java

📁 介绍有关java的资料 课件 相当一本书籍 里面都是很基础的知识
💻 JAVA
字号:
//[实例5.1]判断回文字符串
// CheckPalindrome.java:  检查一个字符串是否是回文
public class CheckPalindrome
{
  // 主方法
  public static void main(String[] args)
  {
    
    System.out.print("Enter a string: "); // Prompt the user to enter a string
    String s = MyInput.readString();

    if (isPalindrome(s))
    {
      System.out.println(s + " is a palindrome");
    }
    else
    {
      System.out.println(s + " is not a palindrome");
    }
  }

  // 检查一个串是不是回文串
  public static boolean isPalindrome(String s)
  {
    
    int low = 0;             // The index of the first character in the string
    
    int up = s.length() - 1;  // The index of the last character in the string

    while (low < up)
    {
      if (s.charAt(low) != s.charAt(up))
        return false; // Not a palindrome

      low++;
      up--;
    }

    return true; // The string is a palindrome
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -