public static void main(String[] arguments) { int yearIn = 2020; int monthIn = 2; if (arguments.length > 0) { monthIn = Integer.parseInt(arguments[0]); } if (arguments.length > 1) { yearIn = Integer.parseInt(arguments[1]); } System.out.println(monthIn + "/" + yearIn + " has " + countDays(monthIn, yearIn) + " days."); } static int countDays(int month, int year) { int count = -1; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: count = 31; break; case 4: case 6: case 9: case 11: count = 30; break; case 2: if (year % 4 == 0) { count = 29; } else { count = 28; } if ((year % 100 == 0) & (year % 400 != 0)) { count = 28; } } return count; }
时间: 2024-02-10 10:33:16 浏览: 40
这段代码是一个Java程序,它接受用户输入的月份和年份,然后计算该月份有多少天。如果用户没有输入月份和年份,则默认使用当前的年份和2月份。程序通过调用countDays函数来计算天数,该函数使用了switch语句来根据月份的不同,返回不同的天数。对于2月份,还加入了判断闰年的逻辑。如果是闰年,则天数为29天,否则为28天。
相关问题
if (arguments.length > 0) { monthIn = Integer.parseInt(arguments[0]); } if (arguments.length > 1) { yearIn = Integer.parseInt(arguments[1]); }
这部分代码是用来获取用户输入的月份和年份的。它首先判断arguments数组的长度是否大于0,如果大于0,则说明用户输入了月份,将其转换为整数并赋值给monthIn变量。接着,它再判断arguments数组的长度是否大于1,如果大于1,则说明用户还输入了年份,将其转换为整数并赋值给yearIn变量。如果用户没有输入年份,则yearIn变量会保持默认值2020。这样,程序就可以根据用户输入的月份和年份来计算该月份有多少天了。
public static void main(String[] args)
As an AI language model, I don't have a specific main method since I don't have a Java environment. However, the main method is the entry point for Java programs. It is a special method that Java looks for when the program is executed, and it must be included in every Java program. The main method has a specific signature, which is:
public static void main(String[] args)
This indicates that the method is public, static, and returns void. The method takes a single argument, which is an array of strings called args. These arguments are passed to the program when it is executed, and they can be used to provide input to the program. The main method is where the program begins and where the initial code is executed.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)