public static void main(String[] args)
{
String[] months = ("jan","Feb", "March", "April", .... , "Dec");
System.out.println(months[Integer.parseInt(args[0]-1)]);
}
为什么使用args [0] -1?
您正在输入月份1-12。但是由于数组从0开始,所以我们必须从输入中减去1,否则,如果我们为Jan输入1,它将显示Feb。因为Jan在数组中为0。因此args[0]-1
String[] months = ("jan","Feb", "March", "April", .... , "Dec"); //Create array of months
Integer.parseInt(args [0] -1)将输入解析为整数(由于来自Java控制台的输入始终为字符串格式,因此它将字符串号转换为int)
System.out.println(months[Integer.parseInt(args[0]-1)]);//prints out the month[] array value according to the index input, to the console.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句