【问题标题】:JavaFX: Missing return statementJavaFX:缺少返回语句
【发布时间】:2017-09-21 18:08:49
【问题描述】:

我有以下代码执行以下操作: - 它采用温度和通道索引并搜索对象列表(包含温度数组)并返回找到温度的对象的索引。 我希望此方法在找到第一个方法时结束,因为这是达到温度的最早时间(已记录)

public int findRow(double targetTemperature, int ch)
{
    //This method takes a double and finds it in the List, it then returns the element in which it is (the row)
    //The element returned can be used with duration.between to find the response time between 2 known values
    for (int i=0; i < readings.size(); i++)
    {
            double compareTemp = readings.get(i).getValue(ch);
            if (compareTemp > targetTemperature)
            {
                System.out.println(readings.get(i).getTimestamp() + "is above target temp for channel " + ch);
                return i;
            }
            else
            {
                System.out.println(readings.get(i).getTimestamp() + "Is not above target temp for channel " + ch);
                return 0;
            }
    }
}

列表包含 TemperatureReadings,这是我创建的具有两个变量的类:
- 双精度值数组
- 当前时间的时间戳(创建数组时)
我正在尝试查找每个频道的响应时间。但是,当我运行上面的代码时,它会说“没有返回语句”,即使两个选项都有返回语句 (if/else)
或者,如果您能帮助我找到更好的方法来找到该通道中的温度(数组索引)达到 X 度的列表的第一次出现,我将非常感激。

实际上,如果可能的话,我不希望它返回 0 以返回错误或“未找到温度”或类似的内容

【问题讨论】:

    标签: java netbeans javafx


    【解决方案1】:

    因为你的 if 语句在你的循环中,如果你的循环没有运行会发生什么? ==> 表示你没有返回语句! 在循环之外添加一个 return 语句,虽然你知道它不能运行这个语句只是因为你确定循环会运行,但是编译器不知道

    【讨论】:

      【解决方案2】:

      Tuyen 是对的。此外,您不想要 else 语句。您将在第一项之后返回。你只需要第一个 if,然后在循环外返回 0;

      试试:

      public int findRow(double targetTemperature, int ch)
      {
          //This method takes a double and finds it in the List, it then returns the element in which it is (the row)
          //The element returned can be used with duration.between to find the response time between 2 known values
          for (int i=0; i < readings.size(); i++)
          {
                  double compareTemp = readings.get(i).getValue(ch);
                  if (compareTemp > targetTemperature)
                  {
                      System.out.println(readings.get(i).getTimestamp() + "is above target temp for channel " + ch);
                      return i;
                  }
          }
          System.out.println(readings.get(i).getTimestamp() + "Is not 
                  above target temp for channel " + ch);
          return -1;
      }
      

      【讨论】:

      • 不正确:如果没有找到任何元素,则返回 0,如果第一个元素符合条件。
      【解决方案3】:

      您的循环不正确:如果第一个元素不符合条件,则该方法将在 else 分支中返回,甚至不检查列表的其他元素。

      您可以删除 else 分支,并制定一个约定(和 javadoc 注释,如果没有找到符合指定条件的项目,则返回 -1)...

      public int findRow(double targetTemperature, int ch) {
          for (int i = 0; i < readings.size(); i++) {
              if (readings.get(i).getValue(ch) > targetTemperature)
                  return i;
          }
          return -1;
      }
      

      ...您可以根据调用方的返回值记录任何内容:

      int channel = 2;
      int ind = findRow(35, channel);
      if (ind >= 0)
          System.out.println(readings.get(ind).getTimestamp() + " is above target temp for channel " + channel);
      else
          System.out.println("Nothing has been found");
      

      同样使用流:

      public int findRow(double targetTemperature, int ch) {
          return IntStream.range(0, readings.size())
                  .filter(i -> readings.get(i).getValue(ch) > targetTemperature)
                  .findFirst()
                  .orElse(-1);
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-30
        • 2017-01-26
        • 2021-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-21
        • 2021-07-11
        • 2017-05-15
        相关资源
        最近更新 更多