?? exercise7_2.java
字號:
/** Exercise7_2.java:
* Rewrite Example 7.1, "Checking Palindromes." Ignore cases.
*/
import javax.swing.JOptionPane;
public class Exercise7_2 {
public static void main(String[] args) {
// Prompt the user to enter a string
String s = JOptionPane.showInputDialog(
null, "Enter a string:",
"Exercise7_2 Input", JOptionPane.QUESTION_MESSAGE);
if (isPalindrome(s)) {
System.out.println(s + " is a palindrome");
}
else {
System.out.println(s + " is not a palindrome");
}
}
/** Check if a string is a palindrome */
public static boolean isPalindrome(String s) {
// The index of the first character in the string
int low = 0;
// The index of the last character in the string
int high = s.length() - 1;
while (low < high) {
if (!equals(s.charAt(low), s.charAt(high)))
return false; // Not a palindrome
low++;
high--;
}
return true; // The string is a palindrome
}
/** Return true if two letters are the same regardless case */
public static boolean equals(char c1, char c2) {
return lowercase(c1) == lowercase(c2);
}
public static char lowercase(char c) {
if (c >= 'A' && c <= 'Z') {
int offset = (int)'a' - (int)'A';
char lowercase = (char)((int)c + offset);
return lowercase;
}
else
return c;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -