Find
the desired month using switch / case statement.
package miss;
public class CaseStatement {
int desiredMonth; // global declaration of the variable.
//This
method will assign respective month to the variable monthOftheYear .
public static String findMonth( int desiredMonth ) {
String monthOftheYear;
switch (desiredMonth) {
case 1:
monthOftheYear = "January";
break;
case 2:
monthOftheYear = "February";
break;
case 3:
monthOftheYear = "March";
break;
case 4:
monthOftheYear = "April";
break;
case 5:
monthOftheYear = "May";
break;
case 6:
monthOftheYear = "June";
break;
case 7:
monthOftheYear = "July";
break;
case 8:
monthOftheYear = "August";
break;
case 9:
monthOftheYear = "September";
break;
case 10: monthOftheYear = "October";
break;
case 11: monthOftheYear = "November";
break;
case 12: monthOftheYear = "December";
break;
default: monthOftheYear = "Enter Valid Month between 1 to 12 ";
break;
}
return monthOftheYear ; // returns the desired month as per the given number when this
method is called.
}
//Let's
find out the desired month by calling the above method inside main method
below.
public static void main(String[] args) {
String thisMonth ;
thisMonth = findMonth(7); //calling the
method providing an int value for the desired month.
System.out.println("This month is : "+thisMonth);
}
}
Answer :
This month is : July
No comments:
Post a Comment