必须是“默认"案件排在最后一个开关? [英] Must the "default" case come last in a switch?

查看:27
本文介绍了必须是“默认"案件排在最后一个开关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 java 中的 switch 语句中,默认"情况是否必须是最后一个?例如,我可以执行以下操作:

In a switch statement in java, is it necessary that the "default" case be the last one? For example, can I do something like the following:

switch(x) {
case A: ....;
default: ....;
case B: ....;
}

推荐答案

没有.但建议放在最后,让代码更易读.下面显示的代码工作正常.

No.. But it is suggested to put it at the end to make the code more readable. The code shown below works fine.

public static void main(String[] args) {

    int i = 5;
    switch (i) {
    default:
        System.out.println("hi");
        break;

    case 0:
        System.out.println("0");
        break;
    case 5:
        System.out.println("5");
        break;
    }
}

O/P : 5

这篇关于必须是“默认"案件排在最后一个开关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆