我正在開發軟件測試課程。我需要創建一個循環遍歷一個字符串,以便找到一個特定的單詞,然後將其與預期結果進行比較。我遇到的問題是我的循環只打印出字符串的第一個單詞。我不能爲了我的生活找出我做錯了什麼。請幫忙。僅循環打印第一個字
這裏是我的代碼:
String input = "Now is the time for all great men to come to the aid of their country";
String tempString = "";
char c = '\0';
int n = input.length();
for(int i = 0; i<n; i++)
{
if(c != ' ')
{
c = input.charAt(i);
tempString = tempString + c;
}
else
{
System.out.println(tempString);
tempString = "";
}
}