JAVA改错题

来源:百度知道 编辑:UC知道 时间:2024/05/16 12:19:30
class Test{
public static void main(String args[]){
long j=2;
switch(j){
default: System.out.println("value is "+j);
case 2: System.out.println("Value is two. ");
case 2+1: System.out.println("Value is three.");
}
}
}

这道题该怎么改错?

远望的小白鲨的改法,其实还是有问题的,因为,switch(j)这里也会出错的,因为switch的参数不能为long类型,只能为int类型。也就是,楼主的代码中一共有两个方面的错误,如下面代码中的标示:
class Test{
public static void main(String args[]){
long j=4;
int myj=(int)j;/////第一,这里必须转换为int型
switch(myj){
default: System.out.println("value is "+j);break;///每个条件必须加上break(当然啦,如果default放最后的话这个条件就不用break了)
case 2: System.out.println("Value is two. ");break;
case 2+1: System.out.println("Value is three.");break;
}
}
}
问题解决了,记得给我分哦~~^_^呵呵……

//大致给你看了下
class Test{
public static void main(String args[]){
long j=2;
switch(j){//switch里的break别忘了
case 2: System.out.println("Value is two. ");break;
case 2+1: System.out.println("Value is three.");break;
default: System.out.println("value is "+j);
}
}
}

class Test{
public static void main(String args[])