package test Created by GrabetsB on 24 02 2015 public class FindTemper

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package test;
/**
* Created by GrabetsB on 24.02.2015.
*/
public class FindTemperatureNearNull
{
public static double closestToZero(double ts[]) throws Exception{
double minTs = -273, maxTs = 5560, min = -273, max = 5560;
if (ts.length==0) return 0;
for (int i = 0; i<ts.length; i++){
if (ts[i]<-273 || ts[i]>5560) throw new Exception();
else {
if (ts[i]<0){
if (ts[i]>min)
min = ts[i];
}
else if (ts[i]<max)
max = ts[i];
}
}
if (max<Math.abs(min))
return max;
else return min;
}
public static void main(String[] args) throws Exception
{
double[] temp = new double[]{-6d, -273d, -1.101245457, 10, 2, 5.123, 1.8246, 0.2, -0.3};
System.out.println(closestToZero(temp));
}
}