我想知道是否有使用toUpperCase的方法來處理StringBuilder?下面是我的代碼,我試圖讓用戶輸入一個短語並將它變成一個首字母縮寫詞。有人通過暗示StringBuilder幫助我,但我無法弄清楚是否有辦法使首字母縮寫爲大寫。任何幫助是極大的讚賞。將stringBuilder強制爲大寫
public class ThreeLetterAcronym {
public static void main(String[] args) {
String threeWords;
int count = 0;
int MAX = 3;
char c;
//create stringbuilder
StringBuilder acronym = new StringBuilder();
Scanner scan = new Scanner(System.in);
//get user input for phrase
System.out.println("Enter your three words: ");
threeWords = scan.nextLine();
//create an array to split phrase into seperate words.
String[] threeWordsArray = threeWords.split(" ");
//loop through user input and grab first char of each word.
for(String word : threeWordsArray) {
if(count < MAX) {
acronym.append(word.substring(0, 1));
++count;
}//end if
}//end for
System.out.println("The acronym of the three words you entered is: " + acronym);
}//end main
}//end class
答案取決於你什麼時候想把它轉換成大寫... – MadProgrammer