日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

switch中default的用法

發(fā)布時(shí)間:2025/5/22 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 switch中default的用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

default什么時(shí)候會(huì)執(zhí)行?default的位置對執(zhí)行結(jié)果有影響嗎?

default只有在case匹配失敗的時(shí)候才會(huì)執(zhí)行

int a=4;switch (a){case 1:System.out.println("1");break;case 2:System.out.println("2");break;case 3:System.out.println("3");break;default:System.out.println("default");break;

打印結(jié)果:default

當(dāng)然也有特殊情況,就是case匹配成功了,但缺少了break語句

int a=3;switch (a){case 1:System.out.println("1");break;case 2:System.out.println("2");break;case 3:System.out.println("3");default:System.out.println("default");break;

打印結(jié)果:

3

default

default的位置對執(zhí)行結(jié)果有沒有影響,關(guān)鍵看default有沒有使用break,先看有break的情況下是什么結(jié)果

int a=4;switch (a){default:System.out.println("default");break;case 1:System.out.println("1");break;case 2:System.out.println("2");break;case 3:System.out.println("3");break;}

打印結(jié)果:default

接下來看看不加break是什么結(jié)果

int a=4;switch (a){default:System.out.println("default");case 1:System.out.println("1");case 2:System.out.println("2");break;case 3:System.out.println("3");break;}

打印結(jié)果:

default
1

2

可以看到不加break的話會(huì)繼續(xù)向下執(zhí)行,直到遇到break或return或switch結(jié)束為止

總結(jié)

以上是生活随笔為你收集整理的switch中default的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。