import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Vowels
{
public static void main(String[] args) throws FileNotFoundException
{
String vowels = "aeiou";
int[] counters = new int[vowels.length()];
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
console.useDelimiter("");
while (console.hasNext())
{
char ch = console.next().charAt(0);
ch = Character.toLowerCase(ch);
if(ch == 'a')
{
counters[0]++;
}
if(ch == 'e')
{
counters[1]++;
}
if(ch == 'i')
{
counters[2]++;
}
if(ch == 'o')
{
counters[3]++;
}
if(ch == 'u')
{
counters[4]++;
}
}
for (int i = 0; i < vowels.length(); i++)
{
System.out.println(vowels.charAt(i) + ": " + counters[i]);
}
}
}
當我運行這些文件時,它會檢測到3 e只有當它應該檢測到兩個都是幾百個時。我沒有看到任何問題,我的代碼會導致問題,請幫助。我假設它必須介於我的分隔符和結尾之間,因爲其餘部分不在本書中。字符不增加while循環中的數組計數器?
您是否嘗試過在while循環中放置'System.out.println(ch);'來查看它是否在做正確的事情?這可能有助於弄清楚。 –
更重要的是,你有沒有嘗試過'String term = console.next();的System.out.println(術語); ch = term.charAt(0);'確定你的'useDelimiter(「」);'是否實際上每次給你一個字符? – AJNeufeld