尝试根据用户输入创建首字母缩略词

Trying to create an Acronym out of user input

你好,我正在做一项作业,我 运行 遇到了一些问题,我希望能得到一些指导...

目的是让用户输入一个短语并从该短语创建一个首字母缩写词。超过三个字的内容将被忽略。

我在首字母缩略词部分遇到问题,我能够获取第一个字符并认为我将循环遍历用户输入并在 space 之后获取字符,但这不起作用.我得到的只是第一个字符,这很明显,因为我首先抓住了它,但我不知道如何 "save" 其他两个字符。非常感谢任何帮助。

*********更新************************ 因此,感谢下面的回答,我在使用 StringBuilder 方面取得了进展。但是,现在如果我输入 "Your Three Words" 输出是:YYYYYTYYYYYWYYYY 这是进步,但我不明白为什么它会多次重复那些第一个字符? 我也编辑了代码。 *********更新******************************

public class ThreeLetterAcronym {

public static void main(String[] args) {
    String threeWords;
    StringBuilder acronym = new StringBuilder();

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter your three words: ");
    threeWords = scan.nextLine();

    for(int count = 0; count < threeWords.length(); count++) {

        acronym.append(threeWords.charAt(0));

            if(threeWords.charAt(count) == ' ') {   
                ++count;
                acronym.append(threeWords.charAt(count));

            }

    }
    System.out.println("The acronym of the three words you entered is: " + acronym);

}
}

您不能保存其他字符,因为 char 应该只存储一个字符。 在这种情况下,您可以使用 StringBuilder

  StringBuilder acronym = new StringBuilder();

然后在您的循环中将其替换为

       String[] threeWordsArray = threeWords.split(" ");

        for(String word : threeWordsArray) {
            acronym.append( word.substring(0, 1) );
        }

**已更新

您将字符存储在 space 中的当前索引处:

char space = threeWords.charAt(count);

然后你比较space的值和整数值3:

if(space < 3)

这几乎肯定不会是真的。您正在询问字符的数值。假设它是一个字母,它至少是 65。我怀疑您的意图是在变量 space.

中存储一些不同的东西