5.1checkpalindrome.java

来自「介绍有关java的资料 课件 相当一本书籍 里面都是很基础的知识」· Java 代码 · 共 42 行

JAVA
42
字号
//[实例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 + =
减小字号Ctrl + -
显示快捷键?