Java 程序查找月份的天数

使用 Else If 语句和带有示例的 Switch Case 编写一个 Java 程序来查找月份的天数。总天数如下:

  • 一月、三月、五月、八月、十月和十二月 = 31 天
  • 二月 = 28 或 29 天
  • 四月、六月、九月和十一月 = 30 天

Java 程序使用 Else If 查找月份的天数

此程序要求用户输入 1 到 12 之间的任何数字,其中 1 = 一月,2 = 二月,3 = 三月,……,12 = 十二月。根据用户输入的整数值,程序将打印该特定月份的天数。为完成此目标,我们正在使用Java Else If 语句

// Java Program to Find Number of Days in a Month
import java.util.Scanner;

public class DaysinMonth1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int month;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : ");
		month = sc.nextInt();	
		
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 )
		{
			System.out.println("\n 31 Days in this Month");  	
		}
		else if ( month == 4 || month == 6 || month == 9 || month == 11 )
		{
			System.out.println("\n 30 Days in this Month");  	
		}  
		else if ( month == 2 )
		{
			System.out.println("\n Either 28 or 29 Days in this Month");  	
		} 
		else
			System.out.println("\n Please enter Valid Number between 1 to 12");
	}
}
Java Program to Find Number of Days in a Month 1

我输入月份 = 11

 Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : 11

 Either 30 Days in this Month

这次我输入月份 = 2

 Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : 2

 Either 28 or 29 Days in this Month

这次,我们输入了错误的值:15

 Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : 15

 Please enter Valid Number between 1 to 12

Java 程序使用 Switch Case 查找月份的天数

这是处理多个条件的理想方法。在此 Java 程序中,我们使用Switch Case来返回月份的天数。

// Java Program to Find Number of Days in a Month
import java.util.Scanner;

public class DaysinMonth2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int month;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : ");
		month = sc.nextInt();	
		
		switch(month)
		{
		  	case 1:
		  	case 3:
			case 5: 	
			case 7:
			case 8:
			case 10:
			case 12:			  	
				System.out.println("\n 31 Days in this Month");
			  	break;
			
			case 4:	
			case 6:
			case 9:
			case 11:			    	
				System.out.println("\n 30 Days in this Month");  
				break;
			
			case 2:
				System.out.println("\n Either 28 or 29 Days in this Month"); 
				break;
			
			default:		  	
				System.out.println("\n Please enter Valid Number between 1 to 12");
		  }
	}
}
Java Program to Find Number of Days in a Month 5

让我尝试不同的值

 Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : 4

 30 Days in this Month

Java 程序使用方法获取月份的天数

Java 程序获取月份的天数,与第一个示例相同。但我们分离了逻辑并将其放在一个单独的方法中。

// Java Program to Find Number of Days in a Month
import java.util.Scanner;

public class DaysinMonth3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int month;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : ");
		month = sc.nextInt();	
		
		DaysinaMonth(month);
	}	
	public static void DaysinaMonth(int month)
	{		
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 )
		{
			System.out.println("\n 31 Days in this Month");  	
		}
		else if ( month == 4 || month == 6 || month == 9 || month == 11 )
		{
			System.out.println("\n 30 Days in this Month");  	
		}  
		else if ( month == 2 )
		{
			System.out.println("\n Either 28 or 29 Days in this Month");  	
		} 
		else
			System.out.println("\n Please enter Valid Number between 1 to 12");
	}
}
 Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : 12

 31 Days in this Month

评论已关闭。