2012-03-15 132 views
-3

我有一個奇怪的問題。我的代碼中的IF條件不起作用。即使條件不成立,它也會進入條件。Android:如果條件不起作用

我在調試模式下檢查值。

這裏是我的代碼:

for (int i = 0; i < Global.shopping_name.size(); i++) {   
     try { 
      double num; 
      long iPart; 
      double fPart; 
      String fractP = ""; 

      // Get user input 
      num = Double.parseDouble(Global.shopping_ammount.get(i)); 

      if(arrIngrName.contains(Global.shopping_name.get(i))); 
      { 
//    Get index of element 
       indx = arrIngrName.indexOf(Global.shopping_name.get(i)); 

       if (arrIngrType.get(indx).equalsIgnoreCase(Global.shopping_type.get(i))) { 

        preValue = Double.parseDouble(arrIngrMeasure.get(i)); 
        newValue = preValue + num; 
        num = newValue;      
       }     
      } 

      iPart = (long) num; 
      fPart = num - iPart; 
      System.out.println("Integer part = " + iPart); 
      System.out.println("Fractional part = " + fPart);    

      ///////////////////////////////////// 

      if (0.062 >= fPart) { 
       fractP = "1/16"; 
      } else if (0.066 >= fPart) { 
      fractP = "1/15"; 
      } else if (0.100 >= fPart) { 
      fractP = "1/10"; 
      } else if (0.125 >= fPart) { 
      fractP ="1/8"; 
      } else if (0.133 >= fPart) { 
      fractP = "2/15"; 
      } else if (0.187 >= fPart) { 
      fractP = "3/16"; 
      } else if (0.200 >= fPart) { 
      fractP ="1/5"; 
      } else if (0.250 >= fPart) { 
      fractP = "1/4"; 
      } else if (0.266 >= fPart) { 
      fractP ="4/15"; 
      } else if (0.300 >= fPart) { 
      fractP = "3/10"; 
      } else if (0.312 >= fPart) { 
      fractP ="5/16"; 
      } else if (0.333 >= fPart) { 
      fractP = "1/3"; 
      } else if (0.375 >= fPart) { 
      fractP ="3/8"; 
      } else if (0.400 >= fPart) { 
      fractP = "2/5"; 
      } else if (0.437 >= fPart) { 
      fractP ="7/16";     
      } else if (0.466 >= fPart) { 
      fractP ="7/15";     
      } else if (0.500 >= fPart) { 
      fractP = "1/2"; 
      } else if (0.533 >= fPart) { 
      fractP = "8/15"; 
      } else if (0.562 >= fPart) { 
      fractP = "9/16";    
      } else if (0.600 >= fPart) { 
      fractP ="3/5";    
      } else if (0.625 >= fPart) { 
      fractP = "5/8";     
      } else if (0.666 >= fPart) { 
      fractP ="2/3"; 
      } else if (0.687 >= fPart) { 
      fractP = "11/16"; 
      } else if (0.700 >= fPart) { 
      fractP ="7/10"; 
      } else if (0.733 >= fPart) { 
      fractP ="11/15"; 
      } else if (0.750 >= fPart) { 
      fractP ="3/4"; 
      } else if (0.800 >= fPart) { 
      fractP ="4/5"; 
      } else if (0.812 >= fPart) { 
      fractP = "13/16";    
      } else if (0.866 >= fPart) { 
      fractP ="13/15"; 
      } else if (0.875 >= fPart) { 
      fractP = "3/4"; 
      } else if (0.900 >= fPart) { 
      fractP ="9/10"; 
      } else if (0.933 >= fPart) { 
      fractP ="14/15"; 
      } else if (0.937 >= fPart) { 
      fractP ="15/16"; 
      }   

      /////////////////////////////////////    

      map = new HashMap<String, String>(); 
       map.put("rowid", "" + (i+1)); 

       if (fPart > 0 && iPart > 0) { 
      map.put("col_1",iPart + " " +fractP + " " + Global.shopping_type.get(i)); 
       } else if (fPart > 0 && iPart == 0) { 
        map.put("col_1",fractP + " " + Global.shopping_type.get(i)); 
      } else { 
       map.put("col_1",iPart + " " + Global.shopping_type.get(i)); 
      }     

       map.put("col_2", " "); 
       map.put("col_3", Global.shopping_name.get(i)); 
       map.put("col_4", "Brukes i "+ Global.shopping_recipe.get(i)); 

      if(arrIngrName.contains(Global.shopping_name.get(i))); 
       { 
//     Get index of element 
        indx = arrIngrName.indexOf(Global.shopping_name.get(i)); 

        if (arrIngrType.get(indx).equalsIgnoreCase(Global.shopping_type.get(i))) { 
//      Do nothing 
         Log.e("SAME", "SAME"); 
        } else { 
         fillMaps.add(map); 
        } 

       } 

       if(!arrIngrName.contains(Global.shopping_name.get(i))); 
       { 
//    
         fillMaps.add(map);      
       } 

//      Add To array 
         arrIngrMeasure.add(Global.shopping_ammount.get(i)); 
         arrIngrName.add(Global.shopping_name.get(i)); 
         arrIngrType.add(Global.shopping_type.get(i)); 

         Log.d("SIZE : ", fillMaps.toString()); 

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
     } 
     } 
       return null;     
     } 

這個代碼不工作:

if(arrIngrName.contains(Global.shopping_name.get(i))); 
      { 
//    Get index of element 
       indx = arrIngrName.indexOf(Global.shopping_name.get(i)); 

       if (arrIngrType.get(indx).equalsIgnoreCase(Global.shopping_type.get(i))) 
       { 

        preValue = Double.parseDouble(arrIngrMeasure.get(i)); 
        newValue = preValue + num; 
        num = newValue; 
        } 
       } 

而且當我添加其他statment它顯示錯誤。

感謝

+0

按照下面的答案,它應該工作。 – Deva 2012-03-15 07:00:49

+0

這個'if(arrIngrName.contains(Global.shopping_name.get(i)));'行 – 2012-03-15 07:02:30

+6

刪除';'因爲如果語句在執行之前終止了塊 – 2012-03-15 07:04:09

回答

2

因爲你在IF語句結尾處有一個分號?

 if(arrIngrName.contains(Global.shopping_name.get(i))); 
+3

謝謝。我愚蠢的錯誤。我花了一個小時。 – 2012-03-15 07:04:45

+1

LOL。 :D發生在每個人身上。 – Rudy 2012-03-15 07:16:11

3

你一直分號;結束時如果聲明。先刪除它。

2
if(arrIngrName.contains(Global.shopping_name.get(i))); 

從訂單的結束; ..