源代码:
package chapter03.sample3_29;
import java.util.Date;
public class systemTimes{
Dtae now = new Date(); //创建表示当前系统时间的Date对象
private int year = now.getYear() + 1900; //获取当前年分
private int month = now.getMonth() + 1; //获取当前月份
private int date = now.getDate(); //获取当前天
private String day;
int temp0 = now.getDay(); //提取当前星期数,并为其转义
switch(temp0){
case 0:
day = "周日";
break;
case 1:
day = "周一";
break;
case 2:
day = "周二";
break;
case 3:
day = "周三";
break;
case 4:
day = "周四";
break;
case 5:
day = "周五";
break;
case 6:
day = "周六";
break;
}
private int hours = now.getHours(); //获取当前小时
int temp = now.getMinutes(); //获取当前分钟
private String min = (temp < 10) ? ("0" + temp) : ("" + temp);
int temp = now.getSeconds(); //获取当前秒
private String sec = (temp < 10) ? ("0" + temp) : ("" + temp);
public void showSystemTimes(){
System.out.println("系统当前时间为:" + year + "年" + month + "月" + "日 " +
day + " " + hours + "时" + min + "分" + sec + "秒");
}
}
编译老是出现switch语句"错误:非法的类型开始",及case 0"错误:孤立的case",求大神帮解答。
答案:1 悬赏:10 手机版
解决时间 2021-03-21 11:10
- 提问者网友:暮烟疏雨之际
- 2021-03-20 23:24
最佳答案
- 五星知识达人网友:孤老序
- 2021-03-20 23:36
switch(temp0){
case 0:
day = "周日";
break;
case 1:
day = "周一";
break;
case 2:
day = "周二";
break;
case 3:
day = "周三";
break;
case 4:
day = "周四";
break;
case 5:
day = "周五";
break;
case 6:
day = "周六";
break;
}上面这个代码本身没有问题,但,不能孤立的放在类声明里面。
将这段代码完整挪到类构造函数里面即可。
case 0:
day = "周日";
break;
case 1:
day = "周一";
break;
case 2:
day = "周二";
break;
case 3:
day = "周三";
break;
case 4:
day = "周四";
break;
case 5:
day = "周五";
break;
case 6:
day = "周六";
break;
}上面这个代码本身没有问题,但,不能孤立的放在类声明里面。
将这段代码完整挪到类构造函数里面即可。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯