我必须编写一个方法,它接受一个字符串并返回一个新的字符串,它复制所有元音并在其间放置一个“b”。 diphtong只有例外,其中“ab”应放在diphtong的前面。在字符串中重复元音并在中间添加字符串
例如:“你好”将返回“hebellobo” “听证会”将返回“habearing”
我已经尝试了我几个小时的代码,但我没有得到任何事情。 嗯,不是任何东西,但不能让它正确运行元音,并没有得到diphtongs。 这是我的代码:
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Enter a string: ");
String s = sc.nextLine();
String originalString = s;
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O')
|| (c == 'o') || (c == 'U') || (c == 'u'))
{
String front = s.substring(0, i);
String back = s.substring(i + 1);
s = front + c + "b" + back;
}
}
System.out.println(originalString);
System.out.println(s);
}
感谢您的帮助!
感谢您的帮助,我现在有以下代码(无扫描):
public static boolean isVowel(char c) {
// TODO exercise 1 task b) part 1
if (c == 'a' || c == 'A' || c == 'Ä' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O'
|| c == 'Ö' || c == 'u' || c == 'U' || c == 'Ü') {
return true;
} else {
return false;
}
}
public static String toB(String text) {
// TODO exercise 1 task b) part 2
StringBuilder b = new StringBuilder();
for (int i = 0; i < text.length() - 1; i++) {
char current = text.charAt(i);
char next = text.charAt(i + 1);
if (isVowel(current)) {
if (isVowel(next)) {
// 1 - Is a vowel followed by a vowel
// Prepend b
b.append("b");
// Write current
b.append(current);
// Write next
b.append(next);
i++; // Skip next vowel
} else {
// 2 - Is a vowel followed by a consonant
b.append(current);
b.append("b");
b.append(current);
}
} else {
// 3 - Is a consonant
b.append(current);
}
}
for (int i = 0; i < text.length() - 1; i++) {
char last = text.charAt(text.length() - 1);
char current = text.charAt(i);
if (isVowel(last)) {
// Case 1
b.append(current);
b.append("b");
b.append(current);
// Case 2 is not possible for last letter
} else {
// Case 3
b.append(last);
}
}
// Here b.toString() is the required string
return b.toString();
}
如果你把例如字“妈妈”时,输出中是“Mobotheberrrrr”,这是完全正常的,除了它出于某种原因重复了最后一个字母'r'。输入“目标”不幸输出“Gboalll”。
不应该是“habearibing” – Henry
你对“双元音”的解释是什么?它是“任何”两个连续的元音还是只有两个连续的元音符合某种条件? – VHS
@亨利你是完全正确的,对不起:) –