import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
public class Details
{
public static void main(String [] args)
{
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
//Adding elements to HashMap
hmap.put(1, "January");
hmap.put(2, "January");
hmap.put(3, "January");
hmap.put(4, "January");
hmap.put(5, "January");
hmap.put(6, "January");
hmap.put(7, "January");
hmap.put(8, "January");
hmap.put(9, "January");
hmap.put(10, "January");
//FOR LOOP
System.out.println("For Loop:");
for (Map.Entry me : hmap.entrySet()) {
System.out.println("Key: "+ me.getKey() + " & Value: " + me.getValue());
}
//WHILE LOOP & ITERATOR
System.out.println("While Loop:");
Iterator iterator = hmap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry me2 = (Map.Entry) iterator.next();
System.out.println("Key: "+ me2.getKey() + " & Value: " + me2.getValue());
}
}
}
我的输出:
For Loop:
Key: 1 & Value: January
Key: 2 & Value: January
Key: 3 & Value: January
Key: 4 & Value: January
Key: 5 & Value: January
Key: 6 & Value: January
Key: 7 & Value: January
Key: 8 & Value: January
Key: 9 & Value: January
Key: 10 & Value: January
While Loop:
Key: 1 & Value: January
Key: 2 & Value: January
Key: 3 & Value: January
Key: 4 & Value: January
Key: 5 & Value: January
Key: 6 & Value: January
Key: 7 & Value: January
Key: 8 & Value: January
Key: 9 & Value: January
Key: 10 & Value: January
我想要这个输出:
1: January 1, 1910
2: January 2, 1910
3: January 3, 1910
4: January 4, 1910
5: January 5, 1910.....
up to
41758: December 28, 2025
41759: December 29, 2025
41760: December 30, 2025
谢谢你的帮助
2条答案
按热度按时间uxh89sit1#
你可以这样做。查看java.time包以获得更多有用的日期/时间类。
像这样的指纹。
另一方面,如果不想使用任何导入的类来执行此操作,则可以按以下方式执行:
创建月份名称数组
创建每月最大天数(非闰年)的数组。
创建闰年方法(稍后解释)。
一个简单的while循环驱动程序来打印日期范围。
只有能被4整除的非世纪年和能被400整除的世纪年才是闰年。首先检查非世纪年,因为它们更频繁地出现。
uyhoqukh2#
这是一种生成所请求的数据的方法,正确地(人们希望)计算闰年等。我将注意到,运行的最后一个数字与示例输出的数字不同。
如果需要将数据存储到某个数据数组中,请替换
output
方法。示例输出