switch條件語(yǔ)句編程Invalid character constant錯(cuò)誤
package com.imooc;
public class Demo1040602 {
public static void main(String[] args){
char detion='廣州';
switch(detion){
case "廣州":
System.out.println("坐長(zhǎng)途汽車(chē)去");
break;
case "上海":
System.out.println("坐高鐵去");
break;
case "北京":
System.out.println("坐飛機(jī)去");
break;
default:
System.out.println("坐火箭去");
}
}
}
char detion='廣州'; ? ? ? 其中這一句是錯(cuò)誤的
2016-01-11
char是可容納單個(gè)字符的數(shù)據(jù)類(lèi)型,‘廣州’已經(jīng)超出了char定義的范圍,故把char detion='廣州'改為String detion='廣州'即可。
2016-01-11
不一一回復(fù)了,你們的回答都很好。不回復(fù)的我已經(jīng)贊了一個(gè)
2016-01-11
感謝各位的熱心解答
2016-01-11
jdk1.7以下的switch條件語(yǔ)句都不可以是String類(lèi)型
2016-01-11
把char detion='廣州'改為String detion="廣州"
2016-01-11
char detion='廣州'; 換成 String detion = "廣州";
2016-01-11
一個(gè)漢字是兩個(gè)字節(jié),'廣州'是四個(gè)字節(jié),已經(jīng)超出char的范圍