尝试理解为什么嵌套的 switch case 语句无法编译。

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

Trying to understand why nested switch case statement is not compiling

问题

以下是您提供的代码的翻译部分:

public String toString(int format) {
  switch (format) {
    case 1:
      return month + "/" + day + "/" + year;
    case 2:
      String pattern = "00";
      DecimalFormat decimalFormat = new DecimalFormat(pattern);
      return decimalFormat.format(month) + "/" +
        decimalFormat.format(day) + "/" +
        year;
    case 3:
      String abbrev = "JanFebMarAprMayJunJulAugSepOctNovDec";
      return abbrev.substring(month * 3 - 3, month * 3 - 1) +
        "/" + day + "/" + year;
    case 4:
      switch (month) {
        case 1:
          return "January" + " " + day + ", " + year;
        case 2:
          return "February" + " " + day + ", " + year;
        case 3:
          return "March" + " " + day + ", " + year;
        case 4:
          return "April" + " " + day + ", " + year;
        case 5:
          return "May" + " " + day + ", " + year;
        case 6:
          return "June" + " " + day + ", " + year;
        case 7:
          return "July" + " " + day + ", " + year;
        case 8:
          return "August" + " " + day + ", " + year;
        case 9:
          return "September" + " " + day + ", " + year;
        case 10:
          return "October" + " " + day + ", " + year;
        case 11:
          return "November" + " " + day + ", " + year;
        case 12:
          return "December" + " " + day + ", " + year;
        default:
          System.out.println("Error please try again");
          break;
      }
    default:
      System.out.println("Error please try again");
  }
}

请注意,此翻译只包括您提供的代码部分,不包含额外的内容。

英文:

So I'm trying to figure out why my IDE is not accepting this piece of code. The error it's giving me is that the return statement is missing. But after doing some digging I found out that when I already have a return statement I don't really need a break. Heres me code.

The month, day, and year are private variables that have already been initialized in the private constructor. Each of the switch cases is supposed to take the date inputs and out the date in a specific format.

public String toString(int format) {
switch (format) {
case 1:
return month + "/" + day + "/" + year;
case 2:
String pattern = "00";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
return decimalFormat.format(month) + "/" +
decimalFormat.format(day) + "/" +
year;
case 3:
String abbrev = "JanFebMarAprMayJunJulAugSepOctNovDec";
return abbrev.substring(month * 3 - 3, month * 3 - 1) +
"/" + day + "/" + year;
case 4:
switch (month) {
case 1:
return "January" + " " + day + ", " + year;
case 2:
return "February" + " " + day + ", " + year;
case 3:
return "March" + " " + day + ", " + year;
case 4:
return "April" + " " + day + ", " + year;
case 5:
return "May" + " " + day + ", " + year;
case 6:
return "June" + " " + day + ", " + year;
case 7:
return "July" + " " + day + ", " + year;
case 8:
return "August" + " " + day + ", " + year;
case 9:
return "September" + " " + day + ", " + year;
case 10:
return "October" + " " + day + ", " + year;
case 11:
return "November" + " " + day + ", " + year;
case 12:
return "December" + " " + day + ", " + year;
default:
System.out.println("Error please try again");
break;
}
default:
System.out.println("Error please try again");
}
}

答案1

得分: 4

你的方法应该返回一个字符串。

这意味着:任何通向你的方法的“退出”的路径,都需要返回一个值。

default:
  System.out.println("错误,请重试");

不返回一个值。它打印了一条消息。

换句话说:你的两个默认情况应该返回一个字符串,比如 “无效输入”。但在真实世界中,这不是你想要的。

你的代码似乎涉及某种日期类。而你的 toString(int) 实现允许出现无效数据。这就是问题的根源所在!你应该在某个时候验证你的字段。但这个时候不是当你被要求进行格式化时。如果你在那个时候这样做,那么该方法应该抛出一个异常,表示:“由于坏数据无法完成我的工作”。

所以,有两个选择:要么你的代码在用户提供输入时验证所有用户输入(并拒绝错误数据),要么你的 toString() 方法在出现无效数据时应该抛出异常。

英文:

Your method is supposed to return a String.

This means: any path that leads to the "exit" of your method, needs to return a value.

default:
System.out.println("Error please try again");

Does not return a value. It prints a message.

In other words: your two default cases should either return a string, such as "invalid input". But in the real world, this isn't what you want.

Your code seems to be about some sort of Date class. And your toString(int) implementation allows for invalid data. That is where your problem starts! You should validate your fields at some point. But that point is not when you are asked to format it. If you do it at that point, that method should rather throw an exception to indicate: "can't do my work because of bad data".

So, two choices: either your code validates all user inputs when the user provides them (and refuses bad data), or your toString() method should throw an exception on invalid data.

huangapple
  • 本文由 发表于 2020年10月14日 11:27:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64346191.html
匿名

发表评论

匿名网友

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

确定