0
String line = JOptionPane.showInputDialog("Enter a full name and surname").toUpperCase();
Scanner l = new Scanner (line);
int index = line.lastIndexOf(" ");
while (l.hasNext())
{
String name = l.next();
char ch = name.charAt(0);
System.out.print(ch);
}
System.out.print(" " + line.substring(index + 1));
用戶輸入以下弗雷德 - 約翰·塞繆爾·史密斯
輸出應該是 - FJS SMITH
的代碼沒有按't不工作,因爲輸出是這樣的:
FJSS SMITH
如何分割最後一個單詞的前三個單詞?
注意:掃描儀不需要用於此目的。檢查[String#split](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-)方法。用空格拆分字符串,使用'for'來循環結果數組,並使用索引來檢查是否到達最後一個單詞。 – BackSlash
而關於你的問題:可能重複[如何大寫字符串中每個單詞的第一個字符](http://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-每個字-IN-A-字符串) – BackSlash