目的は、文字列で受け取ってそれぞれに対応する1と0の文字列を返すことです。たとえば「a」の場合は「100000」です。しかし、それはコンパイルを拒否し、私は何が間違っているのか分かりません。エラーは実際にはすべての行にポップアップしています。私はJavaの根本的なルールを破ったのか、それともなぜ私に怒っているのか分かりません。どんな方向または先端も膨らみます。私は大規模なコードブロックを謝罪します。一度コードの中で他のシリーズにヒットしたら、2番目のコードはすべて同じです。ここで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エラーのように続けます。
'switch'はJavaの予約語です。独自のメソッド名には使用できません。 – GriffeyDog
あなたは 'String x'と' char x'も持っています。 –
*常に*最初のコンパイラエラーで起動します。 – EJoshuaS