package Chapter8;import javax.swing.JOptionPane;public class CheckPalindrome {/** Main method */public static void main(String[] args) {// Prompt the user to enter a stringString s = JOptionPane.showInputDialog("Enter a string:");// Declare and initialize output stringString output = "";if (isPalindrome(s))output = s + " is a palindrome";elseoutput = s + " is not a palindrome";// Display the resultJOptionPane.showMessageDialog(null, output);}/** Check if a string is a palindrome */public static boolean isPalindrome(String s) {// The index of the first character in the stringint low = 0;// The index of the last character in the stringint high = s.length() - 1;while (low < high) {if (s.charAt(low) != s.charAt(high))return false; // Not a palindromelow++;high--;}return true; // The string is a palindrome}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。