我希望实现一个Android应用程序,它根据温度和湿度这两个参数计算热量指数。我可以从 JSON 中提取两个变量,并将它们与下表进行比较:
Heat Index Table http://www.eurometeo.com/img/humidex.gif
我正在考虑将此表存储为Matrix,bun我不知道如何比较数据并返回适当的值。
提前感谢您的帮助。
此致 泰特斯
答案 0 :(得分:0)
这是一个选项:
public class HeatIndexTable {
// TODO write unittests for all methods in this class
public static int minHumidity = 25;
public static int maxHumidity = 100;
public static int humidityStep = 5;
public static int minTemperature = 22;
public static int maxTemperature = 42;
/** converts a humidity percentage to an index in our array */
int humidityToIndex(int humidity) {
if (humidity < minHumidity || humidity > maxHumidity || humidity % humidityStep != 0) {
throw new IllegalArgumentException("Cannot handle humidity " + humidity);
}
return (humidity - minHumidity) / humidityStep;
}
/** converts a temperature in degrees to an index in our array */
int temperatureToIndex(int temperature) {
if (temperature < minTemperature || temperature > maxTemperature) {
throw new IllegalArgumentException("Cannot handle temperature " + temperature);
}
return temperature - minTemperature;
}
int[][] heatIndex = new int[][] {
{ 22, 22, 22, 22, 23, /* etc. */ 31 },
{ 23, 23 /* etc. */ }
// TODO fill in the rest
};
public int getHeatIndex(int temperature, int humidity) {
return heatIndex[temperatureToIndex(temperature)][humidityToIndex(humidity)];
}
}
有了这个,你可以去:
System.out.println(new HeatIndexTable().getHeatIndex(22, 45)); // prints 23
希望它有所帮助。