在這個java程序中一切正常,但最後我必須得到字符長度匹配的字數,但我不能如何得到它?java中字符串的常見長度
Scanner input = new Scanner(System.in);
String s1 = "Enter the name 1:";
System.out.println(s1);
s1 = input.next();
String s2 = "Enter the name 2:";
System.out.println(s2);
s2 = input.next();
if (s1.equals(s2)) {
System.out.println("They match");
} else {
System.out.println("They dont match");
}
char[] c = s1.toCharArray();
char[] d = s2.toCharArray();
for (char i = 0; i < c.length; i++) {
for (char j = 0; j < d.length; j++) {
if (c[i] == d[j]) {
System.out.println("The number of letters matched are :" + c[i]);
}
}
}
System.out.println("The number of letters matched are :" + c.length);
縮進你最好的朋友。或者他們應該是。 – Tommy
你的意思是說你必須得到「字符長度匹配的字數」,但是你的代碼試圖計算兩個字符串有共同的字符數,這是不完全相同的事情。下定決心! – ccdan