Introduction
In this tutorial we will learn how you can create a java program using
switch which help you find , number of days with just 3-3 clicks.yeah sound
weired but its true.
Let get begin with coding part
Before we start writing code ,we first understand the theory of program, don't
worry i will not bore you much.
So in this program we have two variable ,one for storing the month and second
one two store the days in a month.we have no need to declare ,12 variables
for 12 different months .As we are using switch statement i will tell
you how you can do it in know time.
After that we put message on screen for user ,so that upon reading that user
will add the month in digits,hmm in digit we will not use name ,let make it
simple for both user and ofcourse you and me.
We will store that in variable month and using switch statement after words
and applying logic in it.logic that you will see in program below. Let jump
there and see what i have for you.
Q) write a java program to find number of days in a month using switch
statement.
So l)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
int month;
int days;
System.out.printf("Enter month in digits : ");
month = in.nextInt();
switch(month)
{
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 2:
days=28;
break;
default:
days=0;
break;
}
if(days==0)
System.out.printf("this month don't exist\n");
else
System.out.printf("Number of days " +month +" is " + days); }
}
So as you clear with the coding part ,how we use one variable for all 12 month .how you like it tell us in comment for sure and if you know any other way then then let us know ,ok
Now its time for seeing how our program will display to users as we are making it for them only ,its easy amd simple for user .
Output:
Enter month in digits : 3
Number of days 3 is 31
Some more questions like this you can find out on our website to check more questions for practice
Click the bold letters
enjoy the coding with Bigfootcode
0 Comments
if you have any doubt let me know in comment