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

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

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;
}
}
}

英文:
  1. static String dayOfProgrammer(int year) {
  2. if(year==1918){
  3. String date= &quot;26:08:&quot;+year;
  4. return date;
  5. }
  6. if(year&lt;1918|| year&gt;1918){
  7. if(year%4==0){
  8. String date= &quot;12:09:&quot;+year;
  9. return date;
  10. }
  11. else{
  12. String date= &quot;13:09:&quot;+year;
  13. return date;
  14. }
  15. }
  16. }

> 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:

  1. static String dayOfProgrammer(int year) {
  2. if (year == 1918) {
  3. String date = "26:08:" + year;
  4. return date;
  5. } else {
  6. if (year % 4 == 0) {
  7. String date = "12:09:" + year;
  8. return date;
  9. } else {
  10. String date = "13:09:" + year;
  11. return date;
  12. }
  13. }
  14. }
英文:

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:

  1. static String dayOfProgrammer(int year) {
  2. if(year==1918){
  3. String date= &quot;26:08:&quot;+year;
  4. return date;
  5. }
  6. else{
  7. if(year%4==0){
  8. String date= &quot;12:09:&quot;+year;
  9. return date;
  10. }
  11. else{
  12. String date= &quot;13:09:&quot;+year;
  13. return date;
  14. }
  15. }
  16. }

答案2

得分: 0

上一个答案来自 @HaroldH,是正确的,但我建议使用更简单的布局。

  1. static String dayOfProgrammer(int year) {
  2. if (year == 1918) {
  3. return "26:08:" + year;
  4. }
  5. else if (year % 4 == 0) {
  6. return "12:09:" + year;
  7. }
  8. else {
  9. return "13:09:" + year;
  10. }
  11. }

或者(因为在return之后没有需要else

  1. static String dayOfProgrammer(int year) {
  2. if (year == 1918) {
  3. return "26:08:" + year;
  4. }
  5. if (year % 4 == 0) {
  6. return "12:09:" + year;
  7. }
  8. return "13:09:" + year;
  9. }

这两个版本的简单布局使得控制流在一瞥之间就能看清楚,并且你可以看到总是会执行一个return

else if (...) 的放置在一行上,不会导致进一步的缩进,强调了代码在选择不同选项之间做了一个简单的顺序选择。

实际上你不需要else - 就像第二个例子中一样。我认为在这种情况下我更喜欢第一个,但没有硬性规定;重要的是最终代码的透明度。

你会注意到我删除了data 变量;它们在这里没有真正的用途。

英文:

The previous answer from @HaroldH is correct, but I'd suggest a simpler layout.

  1. static String dayOfProgrammer(int year) {
  2. if (year == 1918) {
  3. return &quot;26:08:&quot; + year;
  4. }
  5. else if (year%4 == 0) {
  6. return &quot;12:09:&quot; + year;
  7. }
  8. else {
  9. return &quot;13:09:&quot; + year;
  10. }
  11. }

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

  1. static String dayOfProgrammer(int year) {
  2. if (year == 1918) {
  3. return &quot;26:08:&quot; + year;
  4. }
  5. if (year%4 == 0) {
  6. return &quot;12:09:&quot; + year;
  7. }
  8. return &quot;13:09:&quot; + year;
  9. }

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
匿名

发表评论

匿名网友

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

确定