2012-09-15 34 views
0

我有一组存储在Map/HashMap中的值。然后我做了一个三重循环来比较这些值。值的比较方法如下:首先获取0-1的值,然后将其与1-x [1-2,1-3,... 1-n]开始的一组值相比较。 IF和ONLY如果(例如0-1)的值大于1-x设定值(例如:1-2,1-3,... 1-n)中的所有其他值,则IF-ELSE声明将触发一个事件。无法获取循环来检查通过循环传递的一组值是否大于某个值

的数据的一个示例在下面的代码段中给出:

import java.util.HashMap; 
import java.util.Map; 

public class CompareSequence{ 

    public static void main(String[] args) 
    { 
     Map<String, Integer> myMap = new HashMap<String, Integer>(); 
     myMap.put("0-1", 33); 
     myMap.put("0-2", 29); 
     myMap.put("0-3", 14); 
     myMap.put("0-4", 8); 
     myMap.put("1-2", 37); 
     myMap.put("1-3", 45); 
     myMap.put("1-4", 17); 
     myMap.put("2-3", 1); 
     myMap.put("2-4", 16); 
     myMap.put("3-4", 18); 

     for(int i = 0; i < 5; i++) 
     { 
      for(int j = i+1; j < 5; j++) 
      { 
       String testLine = i+"-"+j; 
       int itemA = myMap.get(testLine); 

       for(int k = j+1; k < 5; k++) 
       { 
        String newLine = j+"-"+k; 
        int itemB = myMap.get(newLine); 

        if(itemA > itemB) 
        { 
         //IF and ONLY all values of item A that is passed through is bigger than item B 
         //THEN trigger an event to group item B with A 
         System.out.println("Item A : " + itemA + " is bigger than item " 
         + newLine + " (" +itemB + ")"); // Printing out results to check the loop 
        } 
        else 
        { 
         System.out.println("Comparison failed: Item " + itemA + " is smaller than " + newLine + " (" + itemB + ")"); 
        } 
       } 
      }  
     }     
    } 
} 

Current Result: 
Get main value for comparison: myMap.get(0-1) = 33 

Get all values related to Key 1-x (set value) .. 
myMap.get(1-2) = 37 // This value is bigger than myMap.get(0-1) = 33 
myMap.get(1-3) = 45 // This value is bigger than myMap.get(0-1) = 33 
myMap.get(1-4) = 17 // This value is smaller than myMap.get(0-1) = 33 

在给定的该示例中,IF-ELSE语句不应让它通过,只有当所有比33小,应该一事件被触发。我应该对IF-ELSE语句做些不同的事情,或者我的循环有问题吗?

Desired Result: 

If((myMap.get(0-1) > myMap.get(1-2)) && (myMap.get(0-1) > myMap.get(1-3)) && (myMap.get(0-1) > myMap.get(1-4))...(myMap.get(0-1) > myMap.get(1-n)) 
{ 
    //Trigger event to group all set values 1-x to value key 0-1 
    //Then delete all set valued related to 1-x from list 
} 

任何意见或帮助将不胜感激。谢谢!

+0

建议你需要对你的期望VS您实际看到的更清楚一点。 – John3136

+0

嗨,约翰,我的问题是获取if-else语句来检查所需条件部分中显示的所有条件。由于只有三个值,所以我可以使用这种方法,但是如果有10个以上的数据比较,这是不可能的。 – Cryssie

回答

1

你可以尝试这样的事:

for (int i = 0; i < 5; i++) { 
    for (int j = i + 1; j < 5; j++) { 
     String testLine = i + "-" + j; 
     int itemA = myMap.get(testLine); 

     boolean greaterThanAll = true; 

     for (int k = j + 1; k < 5; k++) { 
      String newLine = j + "-" + k; 
      int itemB = myMap.get(newLine); 

      if (itemA <= itemB) { 
       System.out.println("Comparison failed: Item " + itemA + " is smaller than " + newLine + " (" + itemB + ")"); 
       greaterThanAll = false; 
       break; 
      } else { 
       System.out.println("Item A : " + itemA + " is bigger than item " 
         + newLine + " (" + itemB + ")"); // Printing out results to check the loop 
      } 
     } 
     if (greaterThanAll) { 
      System.out.println(testLine + "=" + itemA + " is greater than all"); 
      //IF and ONLY all values of item A that is passed through is bigger than item B 
      //THEN trigger an event to group item B with A 
     } 
    } 
} 
+0

嘿assylias,它接近我所需要的,但它是当下一组值比较时...例如:myMap.get(0-2)= 29> myMap.get(2-3)= 1 && myMap.get (0-2)= 29> myMap.get(2-3)= 16应该是true。除非所有值都大于myMap.get(0-1)= 33,否则它不返回结果。 – Cryssie

+0

我看到以下输出:'项目A:29大于项目2-3(1)'then'项目A:29大于项目2-4(16)',导致'0-2 = 29大于全部' - 所以对于'0-2',所有的大小都是正确的。不知道你在问什么... – assylias

+0

我的错误assylias。我在for循环之外放置了boolean greaterThanAll = true,这导致了我的错误。你的方法完美地工作。这是我最后的LOL的人为错误。谢谢! – Cryssie