在JAVA Count Letters计划中获得错误的数字

时间:2013-11-30 17:50:47

标签: java count

任何人都可以告诉我我的代码有什么问题,为什么我没有得到正确的字母数?

此程序读取文本文件并计算每个英文字母,A-Z和a-z,不区分大小写。

感谢您的帮助。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Solution {
    private static int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
    public static void print(){
        int[] in = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
        for (int i = 0; i < in.length; i++){
            System.out.println(in[i]);
        }
    }
    public static void main(String[] args) throws FileNotFoundException{

        File file = new File("t.txt");
        Scanner scan = new Scanner(file);
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            line = line.toLowerCase();
            for (int i = 0; i < line.length(); i++) {
                switch(line.charAt(i)) {
                    case 'a': a++;break;
                    case 'b': b++;break;
                    case 'c': c++;break;
                    case 'd': d++;break;
                    case 'e': e++;break;
                    case 'f': f++;break;
                    case 'g': g++;break;
                    case 'h': h++;break;
                    case 'i': i++;break;
                    case 'j': j++;break;
                    case 'k': k++;break;
                    case 'l': l++;break;
                    case 'm': m++;break;
                    case 'n': n++;break;
                    case 'o': o++;break;
                    case 'p': p++;break;
                    case 'q': q++;break;
                    case 'r': r++;break;
                    case 's': s++;break;
                    case 't': t++;break;
                    case 'u': u++;break;
                    case 'v': v++;break;
                    case 'w': w++;break;
                    case 'x': x++;break;
                    case 'y': y++;break;
                    case 'z': z++;break;
                }
            }
        }
        print();        
    }
}

2 个答案:

答案 0 :(得分:3)

问题在于,当遇到i时,会增加循环的变量,而不是数组中的变量。所以你会跳过信件。

更改为:

for (int counter = 0; counter < line.length(); counter++) {
                switch(line.charAt(counter)) {

答案 1 :(得分:1)

你的问题在于使用变量i

在你的for循环中,索引计数器是i,i也是一个变量,用于计算字母'i'的出现次数。使用这个主要方法,它会起作用。

public static void main(String[] args) throws FileNotFoundException{

    File file = new File("t.txt");
    Scanner scan = new Scanner(file);
    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        line = line.toLowerCase();
        for (int index = 0; index < line.length(); index++) {
            switch(line.charAt(index)) {
                case 'a': a++;break;
                case 'b': b++;break;
                case 'c': c++;break;
                case 'd': d++;break;
                case 'e': e++;break;
                case 'f': f++;break;
                case 'g': g++;break;
                case 'h': h++;break;
                case 'i': i++;break;
                case 'j': j++;break;
                case 'k': k++;break;
                case 'l': l++;break;
                case 'm': m++;break;
                case 'n': n++;break;
                case 'o': o++;break;
                case 'p': p++;break;
                case 'q': q++;break;
                case 'r': r++;break;
                case 's': s++;break;
                case 't': t++;break;
                case 'u': u++;break;
                case 'v': v++;break;
                case 'w': w++;break;
                case 'x': x++;break;
                case 'y': y++;break;
                case 'z': z++;break;
            }
        }
    }
    print();        
}

完成后也不要忘记关闭扫描仪。

相关问题