如何用大量的if语句简化我的方法

时间:2016-05-17 18:09:57

标签: java

我的代码应该读取输入文件并计算文件中每个字符的用法,然后将它们全部打印出来。代码有效但我知道必须有一种方法可以减少所有if语句。现在我正在制作阅读方法和一个输出方法,所以它不是用我的主方法写的。任何和所有建议都很有帮助,谢谢。

import java.io.FileReader;
import java.io.IOException;

public class CharacterCounts {

    static int nl = 0;
    static int sp = 0;
    static int ex = 0;
    static int ap = 0;
    static int cm = 0;
    static int hy = 0;
    static int pd = 0;
    static int cn = 0;
    static int sm = 0;
    static int qu = 0;
    static int a = 0;
    static int b = 0;
    static int c = 0;
    static int d = 0;
    static int e = 0;
    static int f = 0;
    static int g = 0;
    static int h = 0;
    static int ii = 0;
    static int j = 0;
    static int k = 0;
    static int l = 0;
    static int m = 0;
    static int n = 0;
    static int o = 0;
    static int p = 0;
    static int q = 0;
    static int r = 0;
    static int s = 0;
    static int t = 0;
    static int u = 0;
    static int v = 0;
    static int w = 0;
    static int x = 0;
    static int y = 0;
    static int z = 0;
    static int A = 0;
    static int B = 0;
    static int C = 0;
    static int D = 0;
    static int E = 0;
    static int F = 0;
    static int G = 0;
    static int H = 0;
    static int I = 0;
    static int J = 0;
    static int K = 0;
    static int L = 0;
    static int M = 0;
    static int N = 0;
    static int O = 0;
    static int P = 0;
    static int Q = 0;
    static int R = 0;
    static int S = 0;
    static int T = 0;
    static int U = 0;
    static int V = 0;
    static int W = 0;
    static int X = 0;
    static int Y = 0;
    static int Z = 0;

    public static void main(String args[]) throws IOException {
        String file = args[0];
        @SuppressWarnings("resource")
        FileReader scanner = new FileReader(file);
        int i;
        while ((i = scanner.read()) != -1) {
            if ((char) i == '\n') {
                nl++;
            } else if ((char) i == ' ') {
                sp++;
            } else if ((char) i == '!') {
                ex++;
            } else if ((char) i == '\'') {
                ap++;
            } else if ((char) i == ',') {
                cm++;
            } else if ((char) i == '-') {
                hy++;
            } else if ((char) i == '.') {
                pd++;
            } else if ((char) i == ':') {
                cn++;
            } else if ((char) i == ';') {
                sm++;
            } else if ((char) i == '?') {
                qu++;
            } else if ((char) i == 'a') {
                a++;
            } else if ((char) i == 'b') {
                b++;
            } else if ((char) i == 'c') {
                c++;
            } else if ((char) i == 'd') {
                d++;
            } else if ((char) i == 'e') {
                e++;
            } else if ((char) i == 'f') {
                f++;
            } else if ((char) i == 'g') {
                g++;
            } else if ((char) i == 'h') {
                h++;
            } else if ((char) i == 'i') {
                ii++;
            } else if ((char) i == 'j') {
                j++;
            } else if ((char) i == 'k') {
                k++;
            } else if ((char) i == 'l') {
                l++;
            } else if ((char) i == 'm') {
                m++;
            } else if ((char) i == 'n') {
                n++;
            } else if ((char) i == 'o') {
                o++;
            } else if ((char) i == 'p') {
                p++;
            } else if ((char) i == 'q') {
                q++;
            } else if ((char) i == 'r') {
                r++;
            } else if ((char) i == 's') {
                s++;
            } else if ((char) i == 't') {
                t++;
            } else if ((char) i == 'u') {
                u++;
            } else if ((char) i == 'v') {
                v++;
            } else if ((char) i == 'w') {
                w++;
            } else if ((char) i == 'x') {
                x++;
            } else if ((char) i == 'y') {
                y++;
            } else if ((char) i == 'z') {
                z++;
            } else if ((char) i == 'A') {
                A++;
            } else if ((char) i == 'B') {
                B++;
            } else if ((char) i == 'C') {
                C++;
            } else if ((char) i == 'D') {
                D++;
            } else if ((char) i == 'E') {
                E++;
            } else if ((char) i == 'F') {
                F++;
            } else if ((char) i == 'G') {
                G++;
            } else if ((char) i == 'H') {
                H++;
            } else if ((char) i == 'I') {
                I++;
            } else if ((char) i == 'J') {
                J++;
            } else if ((char) i == 'K') {
                K++;
            } else if ((char) i == 'L') {
                L++;
            } else if ((char) i == 'M') {
                M++;
            } else if ((char) i == 'N') {
                N++;
            } else if ((char) i == 'O') {
                O++;
            } else if ((char) i == 'P') {
                P++;
            } else if ((char) i == 'Q') {
                Q++;
            } else if ((char) i == 'R') {
                R++;
            } else if ((char) i == 'S') {
                S++;
            } else if ((char) i == 'T') {
                T++;
            } else if ((char) i == 'U') {
                U++;
            } else if ((char) i == 'V') {
                V++;
            } else if ((char) i == 'W') {
                W++;
            } else if ((char) i == 'X') {
                X++;
            } else if ((char) i == 'Y') {
                Y++;
            } else if ((char) i == 'Z') {
                Z++;
            }

        }
        if (nl != 0) {
            System.out.printf("'/n' %d\n", nl);
        }
        if (sp != 0) {
            System.out.printf("' ' %d\n", sp);
        }
        if (ex != 0) {
            System.out.printf("'!' %d\n", ex);
        }
        if (ap != 0) {
            System.out.printf("''' %d\n", ap);
        }
        if (cm != 0) {
            System.out.printf("',' %d\n", cm);
        }
        if (hy != 0) {
            System.out.printf("'-' %d\n", hy);
        }
        if (pd != 0) {
            System.out.printf("'.' %d\n", pd);
        }
        if (cn != 0) {
            System.out.printf("':' %d\n", cn);
        }
        if (sm != 0) {
            System.out.printf("';' %d\n", sm);
        }
        if (qu != 0) {
            System.out.printf("'?' %d\n", qu);
        }
        if (A != 0) {
            System.out.printf("'A' %d\n", A);
        }
        if (B != 0) {
            System.out.printf("'B' %d\n", B);
        }
        if (C != 0) {
            System.out.printf("'C' %d\n", C);
        }
        if (D != 0) {
            System.out.printf("'D' %d\n", D);
        }
        if (E != 0) {
            System.out.printf("'E' %d\n", E);
        }
        if (F != 0) {
            System.out.printf("'F' %d\n", F);
        }
        if (G != 0) {
            System.out.printf("'G' %d\n", G);
        }
        if (H != 0) {
            System.out.printf("'H' %d\n", H);
        }
        if (I != 0) {
            System.out.printf("'I' %d\n", I);
        }
        if (J != 0) {
            System.out.printf("'J' %d\n", J);
        }
        if (K != 0) {
            System.out.printf("'K' %d\n", K);
        }
        if (L != 0) {
            System.out.printf("'L' %d\n", L);
        }
        if (M != 0) {
            System.out.printf("'M' %d\n", M);
        }
        if (N != 0) {
            System.out.printf("'N' %d\n", N);
        }
        if (O != 0) {
            System.out.printf("'O' %d\n", O);
        }
        if (P != 0) {
            System.out.printf("'P' %d\n", P);
        }
        if (Q != 0) {
            System.out.printf("'Q' %d\n", Q);
        }
        if (R != 0) {
            System.out.printf("'R' %d\n", R);
        }
        if (S != 0) {
            System.out.printf("'S' %d\n", S);
        }
        if (T != 0) {
            System.out.printf("'T' %d\n", T);
        }
        if (U != 0) {
            System.out.printf("'U' %d\n", U);
        }
        if (V != 0) {
            System.out.printf("'V' %d\n", V);
        }
        if (W != 0) {
            System.out.printf("'W' %d\n", W);
        }
        if (X != 0) {
            System.out.printf("'X' %d\n", X);
        }
        if (Y != 0) {
            System.out.printf("'Y' %d\n", Y);
        }
        if (Z != 0) {
            System.out.printf("'Z' %d\n", Z);
        }
        if (a != 0) {
            System.out.printf("'a' %d\n", a);
        }
        if (b != 0) {
            System.out.printf("'b' %d\n", b);
        }
        if (c != 0) {
            System.out.printf("'c' %d\n", c);
        }
        if (d != 0) {
            System.out.printf("'d' %d\n", d);
        }
        if (e != 0) {
            System.out.printf("'e' %d\n", e);
        }
        if (f != 0) {
            System.out.printf("'f' %d\n", f);
        }
        if (g != 0) {
            System.out.printf("'g' %d\n", g);
        }
        if (h != 0) {
            System.out.printf("'h' %d\n", h);
        }
        if (ii != 0) {
            System.out.printf("'i' %d\n", ii);
        }
        if (j != 0) {
            System.out.printf("'j' %d\n", j);
        }
        if (k != 0) {
            System.out.printf("'k' %d\n", k);
        }
        if (l != 0) {
            System.out.printf("'l' %d\n", l);
        }
        if (m != 0) {
            System.out.printf("'m' %d\n", m);
        }
        if (n != 0) {
            System.out.printf("'n' %d\n", n);
        }
        if (o != 0) {
            System.out.printf("'o' %d\n", o);
        }
        if (p != 0) {
            System.out.printf("'p' %d\n", p);
        }
        if (q != 0) {
            System.out.printf("'q' %d\n", q);
        }
        if (r != 0) {
            System.out.printf("'r' %d\n", r);
        }
        if (s != 0) {
            System.out.printf("'s' %d\n", s);
        }
        if (t != 0) {
            System.out.printf("'t' %d\n", t);
        }
        if (u != 0) {
            System.out.printf("'u' %d\n", u);
        }
        if (v != 0) {
            System.out.printf("'v' %d\n", v);
        }
        if (w != 0) {
            System.out.printf("'w' %d\n", w);
        }
        if (x != 0) {
            System.out.printf("'x' %d\n", x);
        }
        if (y != 0) {
            System.out.printf("'y' %d\n", y);
        }
        if (z != 0) {
            System.out.printf("'z' %d\n", z);
        }

    }
}

1 个答案:

答案 0 :(得分:5)

使用Map<Character, Integer>可以轻松改善这一点。将您的声明更改为:

static final Map<Character, Integer> VALUES = new HashMap<>();

然后您的代码正文可以更改为:

Integer val = VALUES.get(i);
if(val == null) VALUES.put(i, 1);
else VALUES.put(i, val + 1);

然后简单地迭代它以显示最后的值。

通过使用LinkedHashMap并使用零预先填充所有所需的值,可以保持顺序或将其限制为仅包含这些字符。

以下是一个示例:

private static final Map<Character, Integer> VALUES = new LinkedHashMap<>();

static{
    for(char c : "\n !\\,-.:;?".toCharArray()){
        VALUES.put(c, 0);
    }
    for(char c = 'a'; c < 'z'; c++){
        VALUES.put(c, 0);
    }
    for(char c = 'A'; c < 'Z'; c++){
        VALUES.put(c, 0);
    }
}

public static void main(String args[]){
    try{
        String file = args[0];
        FileReader scanner = new FileReader(file);
        int i;
        while ((i = scanner.read()) != -1) {
            Integer val = VALUES.get((char)i);
            if(val != null) VALUES.put((char)i, val + 1);
        }
        for(Map.Entry<Character, Integer> entry : VALUES.entrySet()){
            if(entry.getValue() > 0)
                System.out.println(entry.getKey() + " " + entry.getValue());
        }
    } catch(IOException ioe){
        ioe.printStackTrace();
    }
}

^未经测试,但至少应该接近。 现在已经过测试,并修复了许多错误。

相同的行为,更易于维护,不太可能包含拼写错误,以及10%的输入。