写一个java程序,输入年份,开始月份和结束月份,可以得到一个输入年份开始的月集合
时间: 2023-06-04 15:08:29 浏览: 35
好的,这是一个技术问题,我可以帮你回答。你可以使用Java中的Calendar类来实现这个功能,具体代码如下:
```java
import java.util.*;
public class GetMonthSet {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
System.out.print("请输入开始月份:");
int startMonth = scanner.nextInt();
System.out.print("请输入结束月份:");
int endMonth = scanner.nextInt();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, startMonth - 1);
Set<String> monthSet = new HashSet<>();
while (calendar.get(Calendar.MONTH) < endMonth - 1) {
int currentMonth = calendar.get(Calendar.MONTH) + 1;
monthSet.add(currentMonth + "月");
calendar.add(Calendar.MONTH, 1);
}
int currentMonth = calendar.get(Calendar.MONTH) + 1;
monthSet.add(currentMonth + "月");
System.out.println(year + "年" + startMonth + "月至" + endMonth + "月的月份集合为:");
System.out.println(monthSet);
}
}
```
你可以将上述代码复制到IDE中运行,也可以直接将其保存为.java文件,并在命令行中通过javac命令编译,再通过java命令运行。希望对你有帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)