程序显示缺少语句。为什么会出现这个错误?

huangapple go评论60阅读模式
英文:

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= &quot;26:08:&quot;+year;
        return date;
        }
         if(year&lt;1918|| year&gt;1918){
            if(year%4==0){
                String date= &quot;12:09:&quot;+year;
                return date;
            }
            else{
                String date= &quot;13:09:&quot;+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&lt;1918|| year&gt;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&lt;1918|| year&gt;1918) for an else and all paths will return:

static String dayOfProgrammer(int year) {
    if(year==1918){
    String date= &quot;26:08:&quot;+year;
    return date;
    }
    else{
        if(year%4==0){
            String date= &quot;12:09:&quot;+year;
            return date;
        }
        else{
            String date= &quot;13:09:&quot;+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 &quot;26:08:&quot; + year;
    }
    else if (year%4 == 0) {
        return &quot;12:09:&quot; + year;
    }
    else {
        return &quot;13:09:&quot; + year;
    }
}

or even (since there's no need for the 'else' after a 'return')

static String dayOfProgrammer(int year) {
    if (year == 1918) {
        return &quot;26:08:&quot; + year;
    }
    if (year%4 == 0) {
        return &quot;12:09:&quot; + year;
    }
    return &quot;13:09:&quot; + 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.

huangapple
  • 本文由 发表于 2020年8月9日 05:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63320197.html
  • java
  • methods
  • string

如何检查Class是否可转换为类。 go 75
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码