我想将多个水质参数与其范围进行比较 我的代码只比较ph值,我想比较每个参数。
代码在这里。
private void QualityOfWater(){
String Ph_value = ph_value.getText().toString();
String ColorHazen = colorHazen.getText().toString();
String Other_element= other_element.getText().toString();
String Turbidity = turbidity.getText().toString();
String phlow="6.5";
String phup="8.5";
String colorlow="5";
String colorup="15";
String turblow="1";
String turbup="5";
String otherlow="500";
String otherup="2000";
if(Ph_value.compareTo(phlow)>0 && Ph_value.compareTo(phup)<0 ||
ColorHazen.compareTo(colorlow)>0 && ColorHazen.compareTo(colorup)<0 ||
Other_element.compareTo(otherlow)>0 && Other_element.compareTo(otherup)<0 ||
Turbidity.compareTo(turblow)>0 && Turbidity.compareTo(turbup)<0 ){
Toast.makeText(this, "water is drinkable.", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "water is not for drinking purpose.", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
您当前代码中最大的问题是您正在尝试使用文本字符串进行数值比较。这不会起作用,因为字符串按字典顺序排序,这意味着根据字典顺序。在每种情况下,这都不会产生正确的数字排序。另外,我认为你想把所有的逻辑都放在一起,因为大概所有这些标准都必须是真的,以便水可以饮用。
int ph_value = Integer.parseInt(ph_value.getText().toString());
int colorHazen = Integer.parseInt(colorHazen.getText().toString());
int otherElement = Integer.parseInt(other_element.getText().toString());
int turbidity = Integer.parseInt(turbidity.getText().toString());
int phLow = 6.5;
int phHigh = 8.5;
int colorLow = 5;
int colorHigh = 15;
int turbLow = 1;
int turbHigh = 5;
int otherLow = 500;
int otherHigh = 2000;
if (ph_value > phLow && ph_value < phHigh &&
colorHazen > colorLow && colorHazen < colorHigh &&
otherElement > otherLow && otherElement < otherHigh &&
turbidity > turbLow && turbidity < turbHigh) {
Toast.makeText(this, "water is drinkable.", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "water is not for drinking purpose.",
Toast.LENGTH_SHORT).show();
}
另请注意,我根据Java命名约定更改了您的变量名称。变量以小写字母开头,并使用驼峰大小写来分隔单词,而不是下划线。