課程
/后端開發(fā)
/Java
/Java入門第一季(IDEA工具)升級版
我就納悶兒了,既然 switch是來判斷后邊的表達式(num),為何值不等于 num,也要執(zhí)行? ?就像例子那樣,num=1 執(zhí)行,等于2/3/4 的時候,為啥還執(zhí)行? 哪來的判斷一說? 那寫不寫case 1/2/3/4 有何意義????
2016-10-30
源自:Java入門第一季(IDEA工具)升級版 4-6
正在回答
不等于num的時候執(zhí)行default,當num=1的時候執(zhí)行case 1,當它執(zhí)行之后沒有遇見break等停止標識他會繼續(xù)執(zhí)行下面的代碼,比如case 2 的輸出。
慕粉4306753 提問者
public class HelloWorld {
? ? public static void main(String[] args) {
/* char today='二';
? ? ? ? ? switch(today){
? ? ? ? ? ? case '一':
? ? ? ? ? ? System.out.println("吃包子");
? ? ? ? ? ? case'二':
? ? ? ? ? ? System.out.println("吃油條");
? ? ? ? ? ? case'三':
? ? ? ? ? ? case'四':
? ? ? ? ? ? case'五':
? ? ? ? ? ? case'六':
? ? ? ? ? ? default:
? ? ? ? ? ? System.out.println("吃主席套餐");
? ? ? ? }*/
? ? ? ??
? ? ? ?/* switch(today){
? ? ? ? ? ? case'一':
? ? ? ? ? ? ? ? System.out.println("吃包子");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? System.out.println("吃油條");
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? System.out.println("吃主席套餐");
? ? ? ? int today=9;
? ? if(today<7){
if(today%2==1){
System.out.println("吃油條");
}else{
System.out.println("吃包子");
}
System.out.println("吃總統(tǒng)套餐");
? ? ? ?
? ? }
每種情況的后面要加break
舉報
0基礎萌新入門第一課,從Java環(huán)境搭建、工具使用、基礎語法開始
1 回答java中的switch問題
3 回答關于Java中的switch
4 回答Java之 switch
1 回答switch的一些疑問
1 回答Java 里的switch case
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2016-10-30
不等于num的時候執(zhí)行default,當num=1的時候執(zhí)行case 1,當它執(zhí)行之后沒有遇見break等停止標識他會繼續(xù)執(zhí)行下面的代碼,比如case 2 的輸出。
2016-10-31
public class HelloWorld {
? ? public static void main(String[] args) {
/* char today='二';
? ? ? ? ? switch(today){
? ? ? ? ? ? case '一':
? ? ? ? ? ? System.out.println("吃包子");
? ? ? ? ? ? case'二':
? ? ? ? ? ? System.out.println("吃油條");
? ? ? ? ? ? case'三':
? ? ? ? ? ? System.out.println("吃包子");
? ? ? ? ? ? case'四':
? ? ? ? ? ? System.out.println("吃油條");
? ? ? ? ? ? case'五':
? ? ? ? ? ? System.out.println("吃包子");
? ? ? ? ? ? case'六':
? ? ? ? ? ? System.out.println("吃油條");
? ? ? ? ? ? default:
? ? ? ? ? ? System.out.println("吃主席套餐");
? ? ? ? }*/
? ? ? ??
? ? ? ?/* switch(today){
? ? ? ? ? ? case'一':
? ? ? ? ? ? case'三':
? ? ? ? ? ? case'五':
? ? ? ? ? ? ? ? System.out.println("吃包子");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case'二':
? ? ? ? ? ? case'四':
? ? ? ? ? ? case'六':
? ? ? ? ? ? ? ? System.out.println("吃油條");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? System.out.println("吃主席套餐");
? ? ? ? ? ? ? ? break;
? ? ? ? }*/
? ? ? ? int today=9;
? ? if(today<7){
if(today%2==1){
System.out.println("吃油條");
}else{
System.out.println("吃包子");
}
}else{
System.out.println("吃總統(tǒng)套餐");
}
? ? ? ?
? ? ? ?
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ? }
}
2016-10-30
每種情況的后面要加break