Create a program that will make the user input the Month, Date and Year as Integers then use switch statemet and concatenation to display it in this format:
Sample output
Month : 6
Day : 13
Year : 2022
June 13, 2022
Note: If the user inputs an invalid Month then you should tell the user that it is invalid by using the default keyword.
Syntax of default keyword is default:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String rez = "";
boolean tr = true;
String monthString = "";
System.out.println("Sample output");
while(tr){
System.out.print("Month : ");
int month = in.nextInt();
switch (month) {
case 1: monthString = "January ";
tr = false;
break;
case 2: monthString = "February ";
tr = false;
break;
case 3: monthString = "March ";
tr = false;
break;
case 4: monthString = "April ";
tr = false;
break;
case 5: monthString = "May ";
tr = false;
break;
case 6: monthString = "June ";
tr = false;
break;
case 7: monthString = "July ";
tr = false;
break;
case 8: monthString = "August ";
tr = false;
break;
case 9: monthString = "September ";
tr = false;
break;
case 10: monthString = "October ";
tr = false;
break;
case 11: monthString = "November ";
tr = false;
break;
case 12: monthString = "December ";
tr = false;
break;
default:
System.out.println("input error please try again");;
break;
}
}
rez += "" + monthString;
System.out.print("Day : ");
int day = in.nextInt();
rez += "" + day + ", ";
System.out.print("Year : ");
int year = in.nextInt();
rez += "" + year;
System.out.println(rez);
}
}
Comments
Leave a comment