Java编程中,程序不走部分if语句
publicstaticvoidmain(String[]args){intyear,month,day,week;//Scannerinput=newScanner(S...
public static void main(String[] args) {
int year,month,day,week; //
Scanner input = new Scanner(System.in);
System.out.print("年月日:");
year = input.nextInt();
month = input.nextInt();
day = input.nextInt();
week = 8;
if(year < 2015 && year > 2015){
System.out.print("输入不合法.");
}else if (year == 2015&&month ==10){
week = (day + 3) % 7;
}else if (year == 2015&&month == 11){
week = (day + 6)%7;
}else if (year == 2015&&month == 12){
week =(day + 1)% 7;
}else if (year ==2016&&month == 1){
week = 5;
}
System.out.printf("星期%d.\n",week);
为什么程序不经过if(year < 2015 && year > 2015){
System.out.print("输入不合法."); 展开
int year,month,day,week; //
Scanner input = new Scanner(System.in);
System.out.print("年月日:");
year = input.nextInt();
month = input.nextInt();
day = input.nextInt();
week = 8;
if(year < 2015 && year > 2015){
System.out.print("输入不合法.");
}else if (year == 2015&&month ==10){
week = (day + 3) % 7;
}else if (year == 2015&&month == 11){
week = (day + 6)%7;
}else if (year == 2015&&month == 12){
week =(day + 1)% 7;
}else if (year ==2016&&month == 1){
week = 5;
}
System.out.printf("星期%d.\n",week);
为什么程序不经过if(year < 2015 && year > 2015){
System.out.print("输入不合法."); 展开
2个回答
展开全部
if(year < 2015 && year > 2015){
改成:
if(year < 2015 || year > 2015){
或
if(year != 2015 ){
后面的else中 year==2015都是多余的,可以删除掉
改成:
if(year < 2015 || year > 2015){
或
if(year != 2015 ){
后面的else中 year==2015都是多余的,可以删除掉
追问
大神再问一个问题,按你方法改了
然后run显示
年月日:2014 5 19
输入不合法.星期8.
怎么把星期8去掉??
追答
week = 8;
if(year < 2015 && year > 2015){
System.out.print("输入不合法.");
}else { //把输出放到另一个分支中就好了
if (month ==10){
week = (day + 3) % 7;
}else if (month == 11){
week = (day + 6)%7;
}else if (month == 12){
week =(day + 1)% 7;
}else { //??
week = 5;
}
System.out.printf("星期%d.\n",week);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询