这样应该可以
Scanner cin=new Scanner(System.in);
System.out.println("任意字符");
String s;
while (!(s=cin.next()).equals("exit")){
switch (s){
case "a":
System.out.println("gg");
break;
default:
System.out.println("输入错误");
break;
}
}
你的switch里面啥都没 肯定报错啊 switch(s) 是将变量s和下面的case进行比较
Scanner的输入是以换行(Enter)作为分割的,归根结底就是字符串。其方法nextInt() nextLong()等也是后期parse的。所以想要char,需要自己对next()读到的行字符串做判断和转换。
switch少了参数,根据你的代码,需要传入一个string类型的字符串
char c =cin.next();
switch默认的是:Int类型
//强制转换类型一下就可以了
public class VowelsAndConsonants { public static void main(String[] args) { for(int i = 0; i < 100; i++) { char c = (char)(Math.random() * 26 + 'a'); System.out.print(c + ": "); switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println("vowel"); break; case 'y': case 'w': System.out.println( "Sometimes a vowel"); break; default: System.out.println("consonant"); } } } } ///:~
public class VowelsAndConsonants { public static void main(String[] args) { for(int i = 0; i < 100; i++) { char c = (char)(Math.random() * 26 + 'a'); System.out.print(c + ": "); switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println("vowel"); break; case 'y': case 'w': System.out.println( "Sometimes a vowel"); break; default: System.out.println("consonant"); } } } } ///:~