它忽略了辅音。 它忽略了任何类型的空间。 它忽略了案例。 它唯一不能忽视的是另一个元音是否发生故障。
这些计算:
AEIOU,
aeiou,
hahehihohu,
Take it out
这些不是:
AEIuO,
Taco is good,
Take it over
这是我到目前为止所做的:
import java.util.Scanner;
public class AEIOU_Counter {
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("vowels.txt");
Scanner input = new Scanner(file);
String fileContent = "";
while (input.hasNext())
{
fileContent += input.next() + " ";
}
input.close();
char[] charArr = fileContent.toCharArray();
int counter = 0;
for (char c : charArr)
{
if(c == 'a' || c == 'e' ||c == 'i' ||c == 'o' ||c == 'u')
counter++;
}
System.out.println("The file " + file + " has AEIOU in order " + counter + " times");
}
}
问题是输出: 文件vowels.txt按顺序有AEIOU 50次
但是,文件vowels.txt包含:
AEIOU aeiou baeiboeu bbbaaaaaa beaeiou caeuoi ajejijoju aeioo
aeiOu ma me mi mo mu take it OUT!
所以正确的输出应该是:
The file vowels.txt has AEIOU in order 8 times
答案 0 :(得分:1)
第一种方法是将输入编辑为尽可能简单。
1. Read input from file
2. toLowerCase() the input (to make "aEiOU" simplar as just "aeiou")
3. Remove all non-vowel characters. (so that 'hahehihohu' becomes 'aeiou')
4. Search for literal string "aeiou" and count occurrances.
第二种方法是单独保留输入,但使用循环和计数器。 '序列'可以是数组,也可以是链表
sequence = [a,e,i,o,u] // (or a->e->i->o->u)
curr_char_of_sequence = 'a'
counter = 0
for each char in the input, loop {
if the char is not a vowel {
continue to next char
}
//see if the vowel is the one we want next
if char == curr_char_of_sequence {
//it is! update whats the next vowel we want.
// ie, if we were looking for an 'a', now look for an 'e'
curr_char_of_sequence = sequence.next
//check to see if we reached the end of the sequence, if so, we found a completed 'aeiou' set
if curr_char_of_sequence == invalid {
counter++
curr_char_of_sequence = 'a'
}
//we found a vowel that isn't the right one, restart the sequence
} else {
curr_char_of_sequence = 'a'
}
}
答案 1 :(得分:0)
正如人们所指出的,你应该使用正则表达式。 这是一个小小的帮助,以这个特定的顺序为你提供每一个AEIOU(不会忽略其间的非元音)
>>> pattern = r'\b.*\b'.join(w)
>>> regex = re.compile(pattern)
>>>
>>> bool(regex.search(s))
True
>>> s = " you who the heck are"
>>> bool(regex.search(s))
False
答案 2 :(得分:0)
当然,它每次都是这些字母中的一个。 (或者或或......)
尝试使用布尔值,如果你有一个字母(“a”),你会寻找下一个(“e”)。
for (char c : charArr)
{
if(c == 'a') {
boolA = true;
}
else if(c=='e') {
if (boolA) {
boolE = true;
}
else {
boolA = false;
boolE = false;
boolI = false;
boolO = false;
boolU = false;
}
}
else if (c=='i') {
if (boolE) {
boolI = true;
}
else {
boolA = false;
boolE = false;
boolI = false;
boolO = false;
boolU = false;
}
//etc, etc ....
}
如果你明白我的意思^^
或者,还有另一种方式(对于懒惰的家伙) 你记得你找到的最后一个valide字符,如果真实的字母跟着它,它就赢了。
char lastChar;
String validLetters = "aeiou";
String myArray = "eiou"; //i removed the first
for (char c : charArr) {
if (c=='a') {
lastChar=='a';
}
else if ( validLetters.contains(c) && lastChar==validLetters.charAt(myArray.indexOf(c)) ) {
lastChar = c; //u understand it, u get the answer ^^
}
else {
lastChar='w' //just a random char, not in AEIOU
}
最后一个更好, 希望它有所帮助,再见:)
答案 3 :(得分:0)
看起来我好人打败了我,但这里有一个布尔想法的完整例子
public static void main(String[] args) {
boolean a = false;
boolean e = false;
boolean i = false;
boolean o = false;
boolean u = false;
int vowelCounter = 0;
String s = "AEIOU aeiou hahehihohu Take it out";
for (int index = 0; index < s.length(); index++) {
Character c = Character.toLowerCase(s.charAt(index));
if (c == 'a') {
a = true;
continue;
}
if (a && c == 'e') {
e = true;
continue;
}
if (a && e && c == 'i') {
i = true;
continue;
}
if (a && e && i && c == 'o') {
o = true;
continue;
}
if (a && e && i && o && c == 'u') {
u = true;
// no continue because we want to exit this if-chain
}
if (a && e && i && o && u) {
vowelCounter++;
a = e = i = o = u = false; // reset
}
}
System.out.printf("The string \"%s\" contains 'aeiou' in order %d times.\n", s, vowelCounter);
// The string "AEIOU aeiou hahehihohu Take it out" contains 'aeiou' in order 4 times.
}