当前位置: 代码迷 >> J2ME >> 一下 小弟我这个代码如何编译出错
  详细解决方案

一下 小弟我这个代码如何编译出错

热度:1647   发布时间:2013-02-25 21:38:07.0
求助一下 我这个代码怎么编译出错啊
class nb{

int i=5;

switch (i){
case 1:
 System.out.println( " 1天");
 break;
case 2:
 System.out.println( " 2天");
 break;
case 3:
 System.out.println( " 3天");
 break;
}


}//class nb

------解决方案--------------------------------------------------------

Java code
switch (i){case 1: System.out.println( " 1天"); break;case 2: System.out.println( " 2天"); break;case 3: System.out.println( " 3天"); break;}
------解决方案--------------------------------------------------------
代码要写在方法里,如果不写在方法里,也可以把代码写在{}括号内


class nb{

int i=5;

void somemethod { //加一个方法
switch (i){
case 1:
 System.out.println( " 1天");
 break;
case 2:
 System.out.println( " 2天");
 break;
case 3:
 System.out.println( " 3天");
 break;
}
}
}

或者
class nb{

int i=5;

{ //加一对花括号
switch (i){
case 1:
 System.out.println( " 1天");
 break;
case 2:
 System.out.println( " 2天");
 break;
case 3:
 System.out.println( " 3天");
 break;
}

}
}
------解决方案--------------------------------------------------------
switch(){}要放到方法中
比如:

Java code
class nb{    public static void main(String[] args) {        int i = 5;        switch (i) {        case 1:            System.out.println(" 1天");            break;        case 2:            System.out.println(" 2天");            break;        case 3:            System.out.println(" 3天");            break;        }    }}
  相关解决方案