判斷一個字元串是否是對稱字元串,例如"abc"不是對稱字元串,"aba"、"abba"、"aaa"、"mnanm"是對稱字元串。是的話輸出”Yes”,否則輸出”No”。
注意:使用循環和判斷語句實作。
package second;
import java.util.Scanner;
public class 判斷對稱性 {
public static void main(String[] args) {
System.out.println("請輸入一個字元串:");
Scanner sc =new Scanner(System.in);
String st= sc.next();
boolean result = true;
int l=st.length();
int head = 0;
int tail = l-1;
int count = (head+tail)/2;
for (int i = 0; i < count; i++) {
if(st.charAt(head++) != st.charAt(tail++)) {
result = false;
}
}
if(result)
{
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
路是自己的,沒必要拿别人的标準衡量自己,它隻配成為墊腳石。