Java 令牌上的语法错误,表示应将其删除
我的源代码是:Java 令牌上的语法错误,表示应将其删除,java,syntax-error,Java,Syntax Error,我的源代码是: System.out.print("Enter an Alphabet: "); char ch = scan.next().charAt(0); switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println(+ch " is a vowel."); break;
System.out.print("Enter an Alphabet: ");
char ch = scan.next().charAt(0);
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(+ch " is a vowel.");
break;
default:
System.out.println(+ch " is a consonant.");
}
但它说它在“是元音”和“是辅音”上有语法错误。有人能告诉我我的代码出了什么问题吗?我认为
+
在变量的错误一侧
public static void main(String... args) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.print("Enter an Alphabet: ");
char ch = scan.next().charAt(0);
System.out.format("'%s' is a %s.", ch, isVowel(ch) ? "vowel" : "consonant");
}
public static boolean isVowel(char ch) {
ch = Character.toLowerCase(ch);
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
System.out.println(ch + " is a vowel.");
您正在错误地构建字符串。语法应该是
ch+“是元音。”
。+
将字符与您定义的以下字符串连接起来。