无论您怎么看,我都是初学者,所以请耐心等待。该项目是关于在代码中包含一个字符串,然后将其中的每个字母加倍,同时将感叹号增加三倍。没有别的是加倍的。它应该是这样的:
The quick brown fox jumps over the lazy dog 3 times!
...然后把它变成这样:
Tthhee qquuiicckk bbrroowwnn ffooxx jjuummppss oovveerr tthhee llaazzyy ddoogg 3 ttiimmeess!!!
这是我试过的代码,虽然它以数字形式打印并且需要一堆循环而不是一个循环来完成:
String s = "The quick brown fox jumps over the lazy dog 3 times!";
String output = "";
int i = 0;
while (i < s.length()) {
char c = s.charAt(i);
if (s.charAt(i) == '!') {
output += c + c + c;
i++;
}
if (Character.isLetter(c) == true) {
output += c + c;
i++;
} else {
i++;
}
System.out.println(output);
}
最佳答案
您确实应该使用 StringBuilder
,但是 char + char
生成的是 char
而不是 String
(它进行数字加法)。你可能想要 output += ""+ c + c + c;
因为它会将 char
转换为 String
然后附加它们. (对于 output += c + c;
行也是如此)
关于java - 在Java中,字符串中的字母如何加倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26641291/