我想制作一个简单的程序来接受用户输入,并在每个字母之间放置空格。例如,用户进入商场,它返回商场(在同一行)。我正在尝试在其中创建一个带有 if 语句的循环。但我认为我需要 CharAt,所以如果字符串的值大于 1,我会为字符串中的每个单个字符声明一个变量(即用户输入) . 然后我会说在每个字母之间放置空格。我在AP计算机科学A,我们正在练习循环。这下的一切,都是我到目前为止所做的。方向在代码上面的注释中。我使用的是eclipse,java。
/**
* Splits the string str into individual characters: Small becomes S m a l l
*/
public static String split(String str) {
for (int i = 0; str.length() > i; i++) {
if (str.length() > 0) {
char space = str.charAt();
}
}
return str;
}