我有一组存储在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
}
任何意见或帮助将不胜感激。谢谢!
建议你需要对你的期望VS您实际看到的更清楚一点。 – John3136
嗨,约翰,我的问题是获取if-else语句来检查所需条件部分中显示的所有条件。由于只有三个值,所以我可以使用这种方法,但是如果有10个以上的数据比较,这是不可能的。 – Cryssie