這段代碼是一系列任務,首先是它將句子中的所有'F'替換爲'f'而不使用.replace()方法。我做了什麼,之後我應該把所有'f'換成空白,我完成了這些,但由於某種原因,我所有的資本'F都變成了一個奇怪的方塊。有任何想法嗎?我知道這是基本的代碼,但嬰兒的步驟。char替換(不使用替換)
這裏是我的奇怪輸出:「有一個名叫[] isher的isherman,他在某個事件中被清除或者有一些ish;直到一個咧嘴笑起來,把這個isherman拉進來。是她。」
謝謝!搶。
public static void main(String[] args) {
// orginal string sentence
String sentence = ("There was a fisherman named Fisher who fished for some fish in a fissure; till a fish with a grin, pulled the fisherman in. Now they’re fishing the fissure for Fisher. ");
// data
char[] originalArray = sentence.toCharArray();
int i = 0;
int sLength = sentence.length();
int positionArray[];
// combining an int to the array position
positionArray = new int[sLength];
/* while loop to check the position of any 'F' or 'f' characters in sentence and identifying it's array position*/
while (i < sLength) {
char charAt = sentence.charAt(i);
if (charAt == 'F') {
originalArray[i] = 0;
positionArray[i] = 1;
}
i++;
}
//redeclaring int i to 0 for the new array
i = 0;
//reassigning the character 'F' or 'f' to just 'f to 'sentence'
sentence = new String(originalArray);
char[] newArray = sentence.toCharArray();
while (i < sLength) {
if (positionArray[i] == 1) {
newArray[i] = 'f';
}
i++;
//redeclaring int i to 0 for the (part e)
i = 0;
//removing every occurance of 'f' (part e)
while (i < sLength) {
if (newArray[i] == 'f' ) {
newArray[i] = ' ';
}
i++;
//printing to console
}
sentence = new String(newArray);
System.out.println(sentence);
}
}
我對實際輸出道歉的是:「有一個名叫[] isher的isherman,他在某個事件中流露出了某種東西;直到一個笑容把他們拉進去。現在他們正在抨擊事件或[]是她。」 – Napstur
相應地編輯你的問題,你可以這樣做;此外,Java版本是什麼? – fge
不清楚你爲什麼需要'positionArray'。只需在'char []'中直接替換字符即可。 –