📄 5.1checkpalindrome.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 + -