开发者

Separating "if else" statements

What I need is to know how to split these two if else sections up.

    public static int NextBday(int Bdays, int days){
int daysleft = 0;
     if (days > Bdays) {
        daysleft= (365开发者_JAVA技巧+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 you can't tell from the mess what I want is to separate the if statements into two different parts but still leave it in the same method. What do I need to do to so that the second if else statement can print by itself without having to show how many days are left?

e.g. "Wow, your birthday is tomorrow!" without the "Your birthday is in 1 days"

and "Happy Birthday" without the "Your birthday is in 0 days"

Keeping it simple would help the most. Does anyone need the full code? I can try to phrase the question differently if anyone's confused. Thank you very much to all who help!


    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;


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;
}


Check for the == cases first, and only check the other two cases if neither if the == cases are true.


The simplest solution would be to add return; statements in the last two if() cases:

    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's answer is cleaner, though.


This sounds like homework so I'll be a little bit vague rather than just giving you the answer.

Try to think about your code in terms of how you would describe the function in English. Then turn that into pseudocode, then into real code.

"If it's someone's birthday today then say happy birthday. Or, if their birthday is tomorrow, say 'Wow'. Otherwise just say how many days remain until their birthday."

Psuedocode:

if birthdayToday
    print happy birthday message
otherwise if birthdayTomorrow
    print wow message
otherwise
    print days remaining message

The last step is left as an exercise for the reader.


I would do it like this, because the most possible case is (> 1) so we put that option the first one.

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!");
}


 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        
 }

OR

 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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜