-1
我正在研究此代碼,該代碼允許用戶輸入可包含空格和連字符的名稱。名稱縮寫
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
String input = scanner.nextLine();
String result = "";
char firstChar = input.charAt(0);
char fUpper = Character.toUpperCase(firstChar);
result = result + fUpper;
for (int i = 1; i < input.length(); i++) {
char currentChar = input.charAt(i);
char cUpper = Character.toUpperCase(currentChar);
char previousChar = input.charAt(i - 1);
char pUpper = Character.toUpperCase(previousChar);
if (pUpper == ' ') {
result = result + "." + cUpper;
}
}
System.out.println("Initials Are: " + result);
}
我遇到的問題是代碼來識別一個連字符,包括它到底縮寫輸出
它現在認識到連字符謝謝你,但我如何將它包括到最終輸出中,所以如果名稱中有一個連字符,我希望它打印出連字符而不是點號 –
我剛更新了我的答案一個將在輸出中包含連字符的代碼片段。 –