String str1 ="印度是一個偉大的國家";
String str2 = "Isgt";
輸出: ndarecouy
你能幫我嗎?
這是我試過的
public static void main (String [] arg) {
String str1 ="India is a great country";
String str2 = "Isgt";
// Expected output = ndarecouy
String result ="";
for(int i =0 ; i <str2.length(); i ) {
String value1 = String.valueOf(str2.charAt(i));
for (int j = 0; j< str1.length() ; j ) {
String value2 = String.valueOf(str1.charAt(i));
if (value1.equalsIgnoreCase(value2)) {
} else {
result = value2;
}
}
}
System.out.println("My program return this output: " result);
}
我的程式回傳此輸出: nnnnnnnnnnnnnnnnnnnnnnddddddddddddddddddddddiiiiiiiiiiiiiiiiiiiiiiii
uj5u.com熱心網友回復:
外回圈應該遍歷str1not的字符str2。在內部回圈中,您錯誤地使用i而不是j作為索引。然而,即使有這些變化,演算法也不會是正確的。
您需要做的是遍歷中的str1每個字符,result如果str2在result. 您可以使用內部回圈來做到這一點,但使用indexOforcontains會更簡單。
我在下面發布了一個示例解決方案,但我建議您在花了幾個小時嘗試自己解決問題之前不要研究它。
public static void main(String[] arg) {
String str1 = "India is not a bad country";
String str2 = "Isgt";
String result = "";
String str2Lower = str2.toLowerCase();
for (int i = 0; i < str1.length(); i ) {
char value1 = Character.toLowerCase(str1.charAt(i));
if (!Character.isSpaceChar(value1)
&& str2Lower.indexOf(value1) == -1
&& result.indexOf(value1) == -1) {
result = value1;
}
}
System.out.println("My program return this output: " result);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333188.html
標籤:细绳
上一篇:決議python中的布爾運算式
