4 回答
TA贡献1735条经验 获得超5个赞
可能用最少的代码行进行最快的解码是通过一个字符串数组,我们称之为monthNames
,包含所有月份的名称。它的长度为 12,并且由于数组索引是从 0 开始的,因此您必须以这种方式获取字符串,例如第 1 个月的字符串:String month = monthNames[m-1];
TA贡献1851条经验 获得超3个赞
其他人提到过,但我也会选择数组
就像是:
String month = "";
int m = 1; // january
String[] months = {"January", "Febuary", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
month = months[m-1];
TA贡献1804条经验 获得超3个赞
switch 语句和字符串数组一样有效。我发现该数组更容易使用!
//Array to hold each month of the year
String monthArray[] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//Final output statement stating the month, day, and year easter will be held.
System.out.println("\nEaster will be on "+monthArray[m-1]+" "+d+", "+y+".");
TA贡献1872条经验 获得超3个赞
我建议使用 switch 语句。这里有一些可以让你开始的事情:
switch(m) {
case 1: month = "January";
break;
case 2: month = "February";
break;
case 3: month = "March";
break;
case 4: month = "April";
break;
...
case 11: month = "November";
break;
default: month = "December";
}
另外,我建议正确缩进代码,这样如果出现任何问题,可以更轻松地阅读和调试。另外,我建议为变量指定有意义的名称。单字母名称没有多大意义,因此很快就会变得非常混乱。
添加回答
举报