所以我的程序確定用戶輸入是否是迴文。但是,如果輸入包含大小寫混合,那麼最終輸出將全部爲小寫。我如何製作它以便輸出原始用戶輸入。在Java中打印原始字符串
當前:用戶輸入= Aa。輸出= aa是迴文。
目標:用戶輸入= Aa。輸出= Aa是迴文。
import java.util.*;
public class Colonies {
static char [] string;
static int length;
private static Scanner scan;
public static void main (String[] args){
scan = new Scanner(System.in);
System.out.println("Please enter a string of characters.");
String s = scan.nextLine();
s = s.toLowerCase();
string = s.toCharArray();
length = string.length;
if (isPalindrome()){
System.out.println(s+" is a palindrome.");
}
else {
System.out.println(s+" is not a palindrome.");
}
}
public static boolean isPalindrome(){
boolean palindrome = true;
for (int c = 0; c < length/2; c++){
if(string[c] != string [length - c - 1]){
palindrome = false;
}
}
if (palindrome){
return true;
} else {
return false;
}
}
}