英文:
The program is showing a missing statement . Why is this error coming?
问题
static String dayOfProgrammer(int year) {
if (year == 1918) {
String date = "26:08:" + year;
return date;
}
if (year < 1918 || year > 1918) {
if (year % 4 == 0) {
String date = "12:09:" + year;
return date;
} else {
String date = "13:09:" + year;
return date;
}
}
}
英文:
static String dayOfProgrammer(int year) {
if(year==1918){
String date= "26:08:"+year;
return date;
}
if(year<1918|| year>1918){
if(year%4==0){
String date= "12:09:"+year;
return date;
}
else{
String date= "13:09:"+year;
return date;
}
}
}
> Blockquote the method accepts an integer value "year" and should return a String value "date". but due to so issue ,it showcases a runtime error of missing return statement when tested against multiple test cases.
答案1
得分: 2
The compiler cannot guarantee that all paths return a value. Change your statement if(year<1918|| year>1918)
to an else
, and all paths will return:
static String dayOfProgrammer(int year) {
if (year == 1918) {
String date = "26:08:" + year;
return date;
} else {
if (year % 4 == 0) {
String date = "12:09:" + year;
return date;
} else {
String date = "13:09:" + year;
return date;
}
}
}
英文:
The compiler can not guarantee all paths return a value. Switch your statementif(year<1918|| year>1918)
for an else
and all paths will return:
static String dayOfProgrammer(int year) {
if(year==1918){
String date= "26:08:"+year;
return date;
}
else{
if(year%4==0){
String date= "12:09:"+year;
return date;
}
else{
String date= "13:09:"+year;
return date;
}
}
}
答案2
得分: 0
上一个答案来自 @HaroldH,是正确的,但我建议使用更简单的布局。
static String dayOfProgrammer(int year) {
if (year == 1918) {
return "26:08:" + year;
}
else if (year % 4 == 0) {
return "12:09:" + year;
}
else {
return "13:09:" + year;
}
}
或者(因为在return
之后没有需要else
)
static String dayOfProgrammer(int year) {
if (year == 1918) {
return "26:08:" + year;
}
if (year % 4 == 0) {
return "12:09:" + year;
}
return "13:09:" + year;
}
这两个版本的简单布局使得控制流在一瞥之间就能看清楚,并且你可以看到总是会执行一个return
。
else if (...)
的放置在一行上,不会导致进一步的缩进,强调了代码在选择不同选项之间做了一个简单的顺序选择。
实际上你不需要else
- 就像第二个例子中一样。我认为在这种情况下我更喜欢第一个,但没有硬性规定;重要的是最终代码的透明度。
你会注意到我删除了data
变量;它们在这里没有真正的用途。
英文:
The previous answer from @HaroldH is correct, but I'd suggest a simpler layout.
static String dayOfProgrammer(int year) {
if (year == 1918) {
return "26:08:" + year;
}
else if (year%4 == 0) {
return "12:09:" + year;
}
else {
return "13:09:" + year;
}
}
or even (since there's no need for the 'else' after a 'return')
static String dayOfProgrammer(int year) {
if (year == 1918) {
return "26:08:" + year;
}
if (year%4 == 0) {
return "12:09:" + year;
}
return "13:09:" + year;
}
The simpler layout of these two versions makes the control-flow obvious at a glance, and you can see there is always a return executed.
The placement of 'else if (…)' on one line that does not cause further indentation emphasizes that the code is making a simple sequence of choices between alternatives.
You don't actually need the 'else' - as in the second example. I regard that choice here as a matter of taste; I think I prefer the first in this case, but there are no hard-and-fast rules; what matters is the transparency of the resulting code.
You'll note I eliminated the 'data' variables; they served no real purpose here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论