Java 无法使循环检查通过循环的一组值是否大于某个值

Java 无法使循环检查通过循环的一组值是否大于某个值,java,if-statement,for-loop,hashmap,Java,If Statement,For Loop,Hashmap,我在Map/HashMap中存储了一组值。然后我做了一个三重for循环来比较这些值。通过这种方式比较值:首先获取0-1的值,然后将其与以1-x[1-2,1-3,…1-n]开头的一组值进行比较。如果且仅当(例如0-1)的值大于1-x集合值中的所有其他值(例如:1-2、1-3、…1-n),IF-ELSE语句将触发事件 下面的代码片段中给出了数据示例: import java.util.HashMap; import java.util.Map; public class CompareSequen

我在Map/HashMap中存储了一组值。然后我做了一个三重for循环来比较这些值。通过这种方式比较值:首先获取0-1的值,然后将其与以1-x[1-2,1-3,…1-n]开头的一组值进行比较。如果且仅当(例如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

任何建议或帮助都将不胜感激。谢谢大家!

您可以尝试以下方法:

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
        }
    }
}
for(int i=0;i<5;i++){
对于(int j=i+1;j<5;j++){
字符串测试线=i+“-”+j;
int itemA=myMap.get(testLine);
布尔值greaterThanAll=true;
对于(int k=j+1;k<5;k++){
字符串换行符=j+“-”+k;
int itemB=myMap.get(换行符);

如果(itemA建议您需要更清楚地了解您预期的内容与实际看到的内容。嗨,John,我的问题是让if-else语句检查所需结果部分中显示的所有条件。由于只有三个值,我可以使用此方法,但如果要比较的值超过10个,则不可能。嗨,assylias,这是接近我需要的,但它是在比较下一组值的时候…例如:myMap.get(0-2)=29>myMap.get(2-3)=1&&myMap.get(0-2)=29>myMap.get(2-3)=16应该是真的。除非所有值都大于myMap.get(0-1)=33,否则它不会返回任何结果。我看到以下输出:
项A:29大于项2-3(1)
然后
项目A:29大于项目2-4(16)
导致
0-2=29的值大于所有值
-因此对于
0-2
,greaterThanAll是正确的。不确定你在问什么…我的错误是assylias。我将布尔值greaterThanAll=true放在for循环之外,这导致了我端的一些错误。你的方法工作得很好。这是我端的人为错误。谢谢!
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
        }
    }
}