我需要一些关于如何对数组内部值进行排序的指导。
因此,对于X和Z加速度计读数,我有2 Arrays
。
我想将它们分为4类优秀,良好,平均和差。
每个类别都有自己的数组。
在对X和Z arrays
的所有值进行排序并将值1和0插入到每个优秀,良好,平均和差阵列之后,我想通过最高等级来自4个类别的值(在每个类别数组中添加数据)。
问题在于if-else statement
没有进入下一个else if
,即使读数小于1且大于-1。
所以到那时为止,即使我疯狂地摇晃我的手机,我总是获得“优秀”等级。
以下是范围:
for(int i = 0; i < ArrayX.size(); i++){
// sumX = (sumX + Integer.parseInt(String.valueOf(ArrayX.get(i))));
// sumY = (sumY + Integer.parseInt(String.valueOf(ArrayY.get(i))));
X = String.valueOf(ArrayX.get(i));
Y = String.valueOf(ArrayY.get(i));
Z = String.valueOf(ArrayZ.get(i));
float valX = Float.parseFloat(X);
float valZ = Float.parseFloat(Z);
if(valX<excellentRate||valX>(-excellentRate))
{
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(1);
}
else if(valX<=goodRate||valX>=(-goodRate)){
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(1);
ArrayExcellent.add(0);
}
else if(valX<=averageRate||valX>=(-averageRate)){
ArrayPoor.add(0);
ArrayAverage.add(1);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
else if(valX<poorRate||valX>(-poorRate)){
ArrayPoor.add(1);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
if(valZ<(0.00f)||valZ>0.00f)
{
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(1);
}
else if(valZ<=(-2.00f)||valZ>=2.00f){
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(1);
ArrayExcellent.add(0);
}
else if(valZ<=(-4.00f)||valZ>=4.00f){
ArrayPoor.add(0);
ArrayAverage.add(1);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
else if(valZ<(-6.00f)||valZ>4.00f){
ArrayPoor.add(1);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
//codes for sending testSet Details
System.out.println(">>>>>>>>>>>>>>>>>>> "+ " TS= " +TS_ID + " TSA= "+ TSA_ID + X + " " + Y + " " + Z);
String jsonOutput = "http://172.20.34.112/IBBTS_WebService_MobileAndDevice/Service1.asmx/setAccReadings";
if (isOnline()) {
requestAccelData(jsonOutput);
}else {
Toast.makeText(ResultSend.this, "Network isn't available", Toast.LENGTH_LONG).show();
}
}
for (int j=0; j<ArrayExcellent.size(); j++){
sumPoor = sumPoor +Integer.parseInt(String.valueOf(ArrayPoor.get(j)));
sumAverage = sumAverage + Integer.parseInt(String.valueOf(ArrayAverage.get(j)));
sumGood = sumGood + Integer.parseInt(String.valueOf(ArrayGood.get(j)));
sumExcellent = sumExcellent + Integer.parseInt(String.valueOf(ArrayExcellent.get(j)));
}
if(sumExcellent>=sumGood && sumExcellent>=sumAverage && sumExcellent>=sumPoor )
grade ="Excellent";
else if(sumGood>=sumExcellent && sumGood>=sumAverage && sumGood>=sumPoor){
grade ="Good";
}
else if(sumAverage>=sumExcellent && sumAverage>=sumGood && sumAverage>=sumPoor){
grade ="Average";
}
else if(sumPoor>=sumExcellent && sumPoor>=sumGood && sumPoor>=sumAverage){
grade ="Poor";
}
Intent intSummary = new Intent(ResultSend.this, ResultSummary.class);
intSummary.putExtra("gradePassed", grade);
intSummary.putExtra("TestName", TestName);
startActivity(intSummary);
}
我也试过过这样的其他方法:
checker = compare(valX, excellentRateX);
if(checker == 1){
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(1);
checker = 0;
checker = compare(valX, goodRateX);
}
else if(checker == -1){
checker = compare(valX, goodRateX);
}
else if(checker == 1){
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(1);
ArrayExcellent.add(0);
checker = 0;
checker = compare(valX, averageRateX);
}
else if(checker == -1){
checker = compare(valX, averageRateX);
}
else if(checker == 1){
ArrayPoor.add(0);
ArrayAverage.add(1);
ArrayGood.add(0);
ArrayExcellent.add(0);
checker = 0;
checker = compare(valX, poorRateX);
}
else if(checker == -1){
checker = compare(valX, poorRateX);
}
else if(checker == 1){
ArrayPoor.add(1);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(0);
checker = 0;
}
checker = compare(valZ, excellentRateZ);
if(checker == 1){
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(1);
checker = 0;
checker = compare(valZ, goodRateZ);
}
else if(checker == -1){
checker = compare(valZ, goodRateX);
}
else if(checker == 1){
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(1);
ArrayExcellent.add(0);
checker = 0;
checker = compare(valZ, averageRateZ);
}
else if(checker == -1){
checker = compare(valZ, averageRateZ);
}
else if(checker == 1){
ArrayPoor.add(0);
ArrayAverage.add(1);
ArrayGood.add(0);
ArrayExcellent.add(0);
checker = 0;
checker = compare(valZ, poorRateZ);
}
else if(checker == -1){
checker = compare(valZ, poorRateZ);
}
else if(checker == 1){
ArrayPoor.add(1);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(0);
checker = 0;
}
public static int compare(float f1, float f2) {
if (f1 < f2 && f1 > -f2){
return 1;
} // value is within the specifications
else{
return -1;
} // value is outside the specifications
}
根据以下建议编辑代码,现在它正在运行!
if(valX <excellentRateX && valX >(-excellentRateX))
{
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(1);
}
else if(valX <= goodRateX && valX >= (-goodRateX)){
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(1);
ArrayExcellent.add(0);
}
else if(valX <= averageRateX && valX>=(-averageRateX)){
ArrayPoor.add(0);
ArrayAverage.add(1);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
else if(valX <= poorRateX && valX >= (-poorRateX)){
ArrayPoor.add(1);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
else {
ArrayPoor.add(1);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
if(valZ<excellentRateZ && valZ>-excellentRateZ)
{
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(1);
}
else if(valZ <= goodRateZ && valZ >= (-goodRateZ)){
ArrayPoor.add(0);
ArrayAverage.add(0);
ArrayGood.add(1);
ArrayExcellent.add(0);
}
else if(valZ <= averageRateZ && valZ>=(-averageRateZ)){
ArrayPoor.add(0);
ArrayAverage.add(1);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
else if(valZ < poorRateZ && valZ>(-poorRateZ)){
ArrayPoor.add(1);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
else {
ArrayPoor.add(1);
ArrayAverage.add(0);
ArrayGood.add(0);
ArrayExcellent.add(0);
}
答案 0 :(得分:2)
在第一个代码段中,我可以看到以下条件:
if(valX<excellentRate||valX>(-excellentRate))
如果excellentRate = 1
则将其解释为
如果valX小于1或valX大于-1
由于每个实数都是“小于1或大于-1”,因此这种情况总是如此。
你应该使用:
if(valX<excellentRate && valX>(-excellentRate))
对其他if语句也这样做。
P.S。 :我的声誉很低,所以不能在评论中指出这一点,因此我正在写一个答案。