有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java当一个方法包含多个返回的if语句时,如何从该方法中获取返回值?

public static double changeToSpeed(String time, int distance)
{
  if(time.indexOf(":") == 1){
    int minutes = time.charAt(0);
    String sec = time.substring(3, 7);
    double seconds = Double.parseDouble(sec);

    double totalTime = 60 * minutes + seconds;
    double endSpeed = distance / totalTime;
    return endSpeed;
  }
  else if(time.indexOf(":") == 2){
    String min = time.substring(0, 2);
    String sec = time.substring(3, 7);

    double minutes = Double.parseDouble(min);
    double seconds = Double.parseDouble(sec);

    double totalTime = 60 * minutes + seconds;
    double endSpeed = distance / totalTime;
    return endSpeed;
  }
  else if(time.indexOf(":") == -1){
    int minutes = 0;
    double seconds = Double.parseDouble(time);

    double totalTime = 60 * minutes + seconds;
    double endSpeed = distance / totalTime;
    return endSpeed;
  }
}

我试图根据“:”在字符串时间中的位置获得不同的返回。这给了我一个在方法的主要部分没有返回值的问题,但是当我这样做时,它给了我一个不同的错误,说我有太多的返回语句。我需要帮助


共 (2) 个答案

  1. # 1 楼答案

    试试这个

    public static double changeToSpeed(String time, int distance)
    {
      if(time.indexOf(":") == 1){
        int minutes = time.charAt(0);
        String sec = time.substring(3, 7);
        double seconds = Double.parseDouble(sec);
    
        double totalTime = 60 * minutes + seconds;
        double endSpeed = distance / totalTime;
    
      }
      else if(time.indexOf(":") == 2){
        String min = time.substring(0, 2);
        String sec = time.substring(3, 7);
    
        double minutes = Double.parseDouble(min);
        double seconds = Double.parseDouble(sec);
    
        double totalTime = 60 * minutes + seconds;
        double endSpeed = distance / totalTime;
    
      }
      else if(time.indexOf(":") == -1){
        int minutes = 0;
        double seconds = Double.parseDouble(time);
    
        double totalTime = 60 * minutes + seconds;
        double endSpeed = distance / totalTime;
    
      }
      return endSpeed;
    }
    
  2. # 2 楼答案

    目前的问题是,如果time.indexOf(":")不返回1、2或-1,则方法不会返回任何内容。在那种情况下你想做什么?例如,您可能希望抛出异常。你应该弄清楚在那种情况下你想发生什么——然后你就可以弄清楚如何实现它

    我还建议从一开始就进行重构:这种方法的大部分都是一次解析;然后对距离和解析的时间做同样的事情。因此,将时间解析提取到单独的方法:

    public static double changeToSpeed(String time, int distance) {
        double totalTime = parseTime(time);
        return distance / totalTime;
    }
    
    private static double parseTime(String time) {
        // Now you *only* have to deal with time in here
    }
    

    此外,这并不能满足您的期望:

    int minutes = time.charAt(0);
    

    。。。例如,对于“1”,这将得到49。当你用Double.parseDouble表示分:秒的分钟部分时,你是真的想要一个double,还是真的想要一个int?你真的期望像.5:20这样的东西意味着50秒吗

    最后,考虑一下你的“…………”的唯一区别。和“.:……”这是你处理时间的方式,真的。在这两种情况下,您都可以只解析一个整数:

    int colon = time.indexOf(':');
    // If we don't have a colon, just assuming it's minutes:seconds in some form
    if (colon != -1) {
        int minutes = Integer.parseInt(time.substring(0, colon));
        double seconds = Double.parseDouble(time.substring(colon + 1));
        return minutes * 60 + seconds;
    } else {
        return Double.parseDouble(time);
    }
    

    现在假设您希望100:30.5为有效时间。如果您真的只希望在位置1或2处有一个结肠,您应该检查:

    if (colon == 1 || colon == 2) {
        int minutes = Integer.parseInt(time.substring(0, colon));
        double seconds = Double.parseDouble(time.substring(colon + 1));
        return minutes * 60 + seconds;
    } else if (colon == -1) {
        return Double.parseDouble(time);
    } else {
        throw new /* some appropriate exception type */
    }