【发布时间】:2018-05-25 22:32:17
【问题描述】:
在这个程序中,我不仅要打印结果,还要将结果保存为字符串。我怎么做? 另外,我如何“返回”可以打印的字符串值? (此程序将删除“ad”字符串中除单词首字母外的所有元音)
public class RemoveVowels {
public static void main(String[] args) {
String a = "";
remove("Remove all vowels from string except the first letters");
System.out.println();
}
static String remove(String a) {
char c = '\0';
String ss = "";
String s = ("" + a.charAt(0));
String sent = ("" + s);
boolean vowel = false;
boolean first = false;
System.out.print(s);
for (int i = 1; i <= (a.length() - 1); i++) {
c = a.charAt(i);
if (a.charAt(i - 1) == ' ')
first = true;
else
first = false;
if (((c == 'a' || c == 'e') || (c == 'i' || c == 'o')) || (c == 'u' || c == 'y'))
vowel = true;
else
vowel = false;
if ((first == true) || (vowel == false)) {
s = ("" + c);
System.out.print(s);
}
}
return s;
}
}
【问题讨论】:
-
您要打印哪个结果?
-
我已经用 System.out.print(s) 打印出来了——但我希望能够保存打印出来的字符串(因为它一次打印一个字符循环)我也想知道如何返回整个打印字符串的值,以便我可以在 main 方法中打印它。
-
更改 s = ("" + c);到 s+=("" + c);但我建议你使用 StringBuilder