我有一组存储在 Map/HashMap 中的值。然后我做了一个三重for循环来比较这些值。这些值以这种方式进行比较:首先获取 0-1 的值,然后将其与以 1-x [1-2, 1-3,...1-n] 开头的一组值进行比较。IF 且仅当(例如 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
}
任何建议或帮助将不胜感激。谢谢!