字符频率程序不起作用

时间:2015-03-25 18:52:42

标签: arrays floating-point character

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


@SuppressWarnings("unused")
public class Input {

    public static void main(String[] args) throws FileNotFoundException {
        @SuppressWarnings("resource")
        String input = new Scanner( new File("characterfreq.txt") ).useDelimiter("\\A").next();
        char[] character = input.toCharArray();
        int size = character.length;
        int i = 0, j = 0;
        float count = 0;

        for (i=0; i < size; ++i) {
            count = 0;
            for (j = 0; j < size; ++j) {
                if (j < i && character[i] == character[j]) {
                    break;
                }
                if (character[j] == character[i]) 
                    count++;
            }
            if (j == size-1) {
                System.out.println(character[i]+" is present "+count/size+" times");
            }
        }
    }
}   

请你帮助我,当我运行代码没有任何反应时,我的代码运行正常,我添加了另一行并弄乱了它,现在我无法弄清楚我做错了什么

1 个答案:

答案 0 :(得分:0)

您在for循环中的第二个if构造的代码中错过了一个左括号,插入括号并且它应该有效:

if (character[j] == character[i]) {
    count++;
}

如果您想要实际频率,还可以更改您的打印语句以仅打印count,而不是count/size