【发布时间】: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 以返回错误或“未找到温度”或类似的内容
【问题讨论】: