gpt4 book ai didi

java - 无法获取循环来检查通过循环传递的一组值是否大于某个值

转载 作者:行者123 更新时间:2023-12-01 15:10:10 27 4
gpt4 key购买 nike

我有一组值存储在 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
}

任何建议或帮助将不胜感激。谢谢你!

最佳答案

你可以尝试这样的事情:

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
}
}
}

关于java - 无法获取循环来检查通过循环传递的一组值是否大于某个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12435764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com