Java - 什么应该是基本的东西是每行都给出错误

时间:2017-09-06 21:03:13

标签: java compiler-errors

目标是接收一串字母并返回一个1和0的字符串,每个字符串对应一个字符串。例如,“a”的“100000”。但它拒绝编译,我不确定是什么问题。实际上每一行都会出现错误。我不确定我是否打破了一些基本的Java规则或为什么它对我不满意。任何方向或提示都会膨胀。我为大型代码块道歉,一旦你在我的代码中点击其他系列,在第二个之后它们都是相同的。

这是我的代码:

package com.google.challenges; 

public class Answer {   

public static String answer(String plaintext) { 
    String[] braille = new String[50];
    String answer = "";
    char j = 'a';

    for(int i=0; i<plaintext.length();i++){
        j = plaintext.charAt(i);
        j = switch(j);
        braille[i] = j;
    }//close for

    for(int i=0; i<plaintext.length(); i++){
        answer = answer+braille[i];
    }
    return answer;

} //close answer
public static String switch (char x)
{
    String a = "100000";
    String b = "110000";
    String c = "100100";
    String d = "100110";
    String e = "100010";
    String f = "110100";
    String g = "110110";
    String h = "110010";
    String i = "010100";
    String j = "010110";
    String k = "101000";
    String l = "111000";
    String m = "101100";
    String n = "101110";
    String o = "101010";
    String p = "111100";
    String q = "111110";
    String r = "111010";
    String s = "011100";
    String t = "011110";
    String u = "101001";
    String v = "111001";
    String w = "010111";
    String x = "101101";
    String y = "101111";
    String z = "101011";
    String cap = "000001";
    String space = "000000";

    if (x == 'a' || x == 'A'){
        if(x=='a'){
            return a;
        }
        return cap + a;
    }
    else if (x == 'b' || x == 'B'){
        if(x=='b')
        {
            return b;
        }
        return cap + b;
    }
    else if (x == 'c' || x == 'C'){
        if(x=='c'){
            return c;
        }
        return cap + c;
    }
    else if (x == 'd' || x == 'D'){
        if(x=='d'){
            return d;
        }
        return cap + d;
    }
    else if (x == 'e' || x == 'E'){
        if(x=='e'){
            return e;
        }
        return cap + e;
    }
    else if (x == 'f' || x == 'F'){
        if(x=='f'){
            return f;
        }
        return cap + f;
    }
    else if (x == 'g' || x == 'G'){
        if(x=='g'){
            return g;
        }
        return cap + g;
    }
    else if (x == 'h' || x == 'H'){
        if(x=='h'){
            return h;
        }
        return cap + h;
    }
    else if (x == 'i' || x == 'I'){
        if(x=='i'){
            return i;
        }
        return cap + i;
    }
    else if (x == 'j' || x == 'J'){
        if(x=='j'){
            return j;
        }
        return cap + j;
    }
    else if (x == 'k' || x == 'K'){
        if(x=='k'){
            return k;
        }
        return cap + k;
    }
    else if (x == 'l' || x == 'L'){
        if(x=='l'){
            return l;
        }
        return cap + l;
    }
    else if (x == 'm' || x == 'M'){
        if(x=='m'){
            return m;
        }
        return cap + m;
    }
    else if (x == 'n' || x == 'N'){
        if(x=='n'){
            return n;
        }
        return cap + n;
    }
    else if (x == 'o' || x == 'O'){
        if(x=='o'){
            return o;
        }
        return cap + o;
    }
    else if (x == 'p' || x == 'P'){
        if(x=='p'){
            return p;
        }
        return cap + p;
    }
    else if (x == 'q' || x == 'Q'){
        if(x=='q'){
            return q;
        }
        return cap + q;
    }
    else if (x == 'r' || x == 'R'){
        if(x=='r'){
            return r;
        }
        return cap + r;
    }
    else if (x == 's' || x == 'S'){
        if(x=='s'){
            return s;
        }
        return cap + s;
    }
    else if (x == 't' || x == 'T'){
        if(x=='t'){
            return t;
        }
        return cap + t;
    }
    else if (x == 'u' || x == 'U'){
        if(x=='u'){
            return u;
        }
        return cap + u;
    }
    else if (x == 'v' || x == 'V'){
        if(x=='v'){
            return v;
        }
        return cap + v;
    }
    else if (x == 'w' || x == 'W'){
        if(x=='w'){
            return w;
        }
        return cap + w;
    }
    else if (x == 'x' || x == 'X'){
        if(x=='x'){
            return x;
        }
        return cap + x;
    }
    else if (x == 'y' || x == 'Y'){
        if(x=='y'){
            return y;
        }
        return cap + y;
    }
    else if (x == 'z' || x == 'Z'){
        if(x=='z'){
            return z;
        }
        return cap + z;
    }
    else{
        return space;
    }
}//close switch
}//close class

这是我的错误:

/Answer.java:12: error: illegal start of expression
j = switch(j);
^
/Answer.java:12: error: not a statement
j = switch(j);
^
/Answer.java:22: error: <identifier> expected
public static String switch (char x)
^
/Answer.java:22: error: illegal start of type
public static String switch (char x)
^
/Answer.java:22: error: <identifier> expected
public static String switch (char x)
^
/Answer.java:22: error: ';' expected
public static String switch (char x)
^
/Answer.java:22: error: illegal start of type
public static String switch (char x)
^
/Answer.java:22: error: <identifier> expected
public static String switch (char x)
^
/Answer.java:23: error: ';' expected
{
^
/Answer.java:24: error: <identifier> expected
String a = "100000";
^
/Answer.java:53: error: illegal start of type
if (x == 'a' || x == 'A'){
^
/Answer.java:53: error: <identifier> expected
if (x == 'a' || x == 'A'){
^
/Answer.java:53: error: ';' expected
if (x == 'a' || x == 'A'){
^
/Answer.java:53: error: illegal start of type
if (x == 'a' || x == 'A'){
^
/Answer.java:53: error: ';' expected
if (x == 'a' || x == 'A'){
^
/Answer.java:59: error: illegal start of type
else if (x == 'b' || x == 'B'){
^
/Answer.java:59: error: ';' expected
else if (x == 'b' || x == 'B'){
^
/Answer.java:59: error: illegal start of type
else if (x == 'b' || x == 'B'){
^
/Answer.java:59: error: ';' expected
else if (x == 'b' || x == 'B'){
^
/Answer.java:59: error: <identifier> expected
else if (x == 'b' || x == 'B'){
^
/Answer.java:59: error: illegal start of type
else if (x == 'b' || x == 'B'){
^
/Answer.java:59: error: <identifier> expected
else if (x == 'b' || x == 'B'){
^
/Answer.java:59: error: ';' expected
else if (x == 'b' || x == 'B'){
^
/Answer.java:60: error: illegal start of type
if(x=='b')
^
/Answer.java:60: error: <identifier> expected
if(x=='b')
^
/Answer.java:60: error: ';' expected
if(x=='b')
^
/Answer.java:60: error: illegal start of type
if(x=='b')
^
/Answer.java:60: error: <identifier> expected
if(x=='b')
^
/Answer.java:60: error: ';' expected
if(x=='b')
^
/Answer.java:64: error: illegal start of type
return cap + b;
^
/Answer.java:64: error: ';' expected
return cap + b;
^
/Answer.java:64: error: illegal start of type
return cap + b;
^
/Answer.java:66: error: class, interface, or enum expected
else if (x == 'c' || x == 'C'){
^
/Answer.java:69: error: class, interface, or enum expected
}
^
/Answer.java:71: error: class, interface, or enum expected
}
^
/Answer.java:75: error: class, interface, or enum expected
}
^
/Answer.java:77: error: class, interface, or enum expected
}
^
/Answer.java:81: error: class, interface, or enum expected
}
^
/Answer.java:83: error: class, interface, or enum expected
}
^
/Answer.java:87: error: class, interface, or enum expected
}
^
/Answer.java:89: error: class, interface, or enum expected
}
^

错误继续,就像另外100行的第89行错误一样。

1 个答案:

答案 0 :(得分:1)

Switch是Java中的保留字。用于switch ... case结构。

您已将方法切换命名为不允许的。

你得到的第一组错误是因为当你使用j = switch(j)时,它会将单词switch视为switch结构的开头,然后告诉你switch(j)不是一个有效的方法使用它。

然后你看到的第二组错误是看到你已经开始声明一个方法,但你没有给它起一个名字。当然你有 - 你称它为开关。但它没有将'switch'这个词看作一个函数名。

然后它就从那里走下坡路。