我需要知道如何拆分这两个 if else 部分。
public static int NextBday(int Bdays, int days){
int daysleft = 0;
if (days > Bdays) {
daysleft= (365+Bdays) - days;
}else if(Bdays > days){
daysleft= Bdays-days;
}if (Bdays == days + 1) {
System.out.println("\nWow, your birthday is tomorrow!");
}else if (Bdays == days){
System.out.println("\nHappy birthday!");
}
System.out.println("\nYour birthday is in " +daysleft+ " days");
return daysleft;
如果你无法从混乱中分辨出我想要的是将if语句分成两个不同的部分但仍然保留在同一个方法中。我需要做什么才能使第二个if else语句可以自行打印而不必显示剩下多少天?
e.g。 “哇,你的生日是明天!”没有“你的生日是1天”
和“生日快乐” 没有“你的生日是0天”
保持简单会有所帮助。有人需要完整的代码吗?如果有人感到困惑,我可以尝试用不同的方式表达问题。非常感谢所有帮助过的人!
答案 0 :(得分:3)
public static int NextBday(int Bdays, int days){
int daysleft = 0;
if (days > Bdays) {
daysleft= (365+Bdays) - days;
}else if(Bdays > days){
daysleft= Bdays-days;
}
if (Bdays == days + 1) {
System.out.println("\nWow, your birthday is tomorrow!");
}else if (Bdays == days){
System.out.println("\nHappy birthday!");
} else
System.out.println("\nYour birthday is in " +daysleft+ " days");
return daysleft;
答案 1 :(得分:1)
if(Bdays - days != 1 && Bdays != days){ //if it's not today or tomorrow
int daysleft;
System.out.println("\nYour birthday is in " + (daysleft = (int)((days > Bdays)?(365+Bdays-days):(Bdays-days))) + " days."); //print the message, and calculate the correct number of days on the spot
return daysleft;
}
else{ //if it is today or tomorrow
System.out.println(((Bdays == days)?"\nHappy Birthday!":"\nWow, your birthday is tomorrow!")); //If it's today, print happy birthday, if it's not today it must be tomorrow.
return Bdays - days;
}
答案 2 :(得分:0)
首先检查==个案,只检查其他两个案例,如果==个案都不是真的。
答案 3 :(得分:0)
最简单的解决方案是增加回报;最后两个if()案例中的陈述:
public static int NextBday(int Bdays, int days){
int daysleft = 0;
if (days > Bdays) {
daysleft= (365+Bdays) - days;
}else if(Bdays > days){
daysleft= Bdays-days;
}if (Bdays == days + 1) {
System.out.println("\nWow, your birthday is tomorrow!");
return daysleft;
}else if (Bdays == days){
System.out.println("\nHappy birthday!");
return daysleft;
}
System.out.println("\nYour birthday is in " +daysleft+ " days");
return daysleft;
Paul Tomblin的回答更清晰。
答案 4 :(得分:0)
这听起来像是家庭作业,所以我会有点含糊,而不仅仅是给你答案。
尝试根据如何用英语描述函数来考虑您的代码。然后将其转换为伪代码,然后转换为实际代码。
“如果今天是某人的生日,那就说生日快乐。或者,如果他们的生日是明天,请说'哇'。否则只要说他们的生日还有多少天。”
伪码:
if birthdayToday
print happy birthday message
otherwise if birthdayTomorrow
print wow message
otherwise
print days remaining message
最后一步是留给读者的练习。
答案 5 :(得分:0)
我会这样做,因为最可能的情况是(> 1)所以我们把那个选项放在第一个。
int diff = Bdays - days;
if (diff > 1){
System.out.println("\nYour birthday is in " +diff+ " days");
}
else if (diff == 1){
System.out.println("\nWow, your birthday is tomorrow!");
}
else{
System.out.println("\nHappy birthday!");
}
答案 6 :(得分:0)
public static int NextBday(int Bdays, int days){
int daysleft = getDaysLeft(Bdays,days);//move logic to a private function
if (daysleft == 1)
System.out.println("\nWow, your birthday is tomorrow!");
else if (daysleft == 0)
System.out.println("\nHappy birthday!");
else
System.out.println("\nYour birthday is in " +daysleft+ " days");
return daysleft;
}
private static int getDaysLeft(int Bdays, int days) {
if (days > Bdays)
return (365 + Bdays) - days;
return Bdays - days; //no need to check for the < condition
}
或强>
public static int NextBday(int Bdays, int days) {
int daysleft = days > Bdays ? 365 + Bdays - days : Bdays - days;
if (daysleft == 1)
System.out.println("\nWow, your birthday is tomorrow!");
else if (daysleft == 0)
System.out.println("\nHappy birthday!");
else
System.out.println("\nYour birthday is in " + daysleft + " days");
return daysleft;
}