如何比较现有列表与来自用户的列表?使用飞镖和扑动



我希望用户输入与每个列表(normalList, protanList,deutanList, tritanList)进行比较

所以,假设如果用户输入是1 - 15正确,输出将是"Normal">

如果用户输入为[15,14,1,2,13,12,3,4,11,10,5,6,9,8,7],则输出为"Protan">

请帮帮我,这是我最后一年的项目,我完全不知道该怎么做

末List不透明盒子= [];//待填充

//假设用户在这里输入了不透明框

//result方法用于比较结果的结果字符串结果(){字符串结果= ";if (listEquals(normalList, opaqueBoxes)) {result = "Normal";} else {result = "Protan";}if (kDebugMode) {打印(结果);}返回结果;}

//这里是正确的列表

var normalList = ["1","2","3","4","5","5","6","7","8","9","10","11","12","13","14","15";];var protanList = ["15","14","1","2","13","12","3","4","11","10","5","6","9","8","7";

);var deutanList = ["1","15","2","3","14","13","4","12","5","6","11","10","7","9","8";

);var trianlist = ["1","2","3","4","5","6","7","15","8","14","9","13","10","11","12";

);

#TO PUT EVERYTHING simple

如果您需要两个列表保持相同的顺序,那么尝试这样做:

bool isSame = a.every((item) => item == b[a.indexOf(item)]);
//List `a` is getting compared with list `b`.

如果你不需要条目的顺序完全相同,你可以试试:

bool isSame = a.every((item) => b.contains(item));
//List `a` is getting compared with list `b`.

现在你可以试着找出你的方法来创建一个结果。并在这个过程中学习。祝你好运。👍

所以我已经在下面的代码中记录了我想说的内容。但这里有一个简短的。

首先,请注意我认为不再支持listEquals了。您必须创建您的过滤条件。

这既简单又复杂。不输入就无法获得变量名它们或编写一个新类。好吧,深入看看我做了什么。我将努力使事情变得简单而坚强。

同样,当用代码库发布问题时,总是使用"代码示例

如果你需要进一步的帮助,请告诉我。

// First, note that I don't think `listEquals` is in support anymore. You will have
// create your filter condition.
// This is easy and complex. There is no way to get a variable name without
// typing them or writing a new class writing a new function. Well, deep in 
// and see what I did. I will try and make Things easy and strong.
void main() {
//   I changed to type `Box` to `String` as we are getting a string input
late List<String> opaqueBoxes = [
"1",
"15",
"2",
"3",
"14",
"13",
"4",
"12",
"5",
"6",
"11",
"10",
"7",
"9",
"8"
];
// Lists to filter against. Added type casting
final List<String> normalList = [
"1",
"2",
"3",
"4",
"5",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15"
];
final List<String> protanList = [
"15",
"14",
"1",
"2",
"13",
"12",
"3",
"4",
"11",
"10",
"5",
"6",
"9",
"8",
"7"
];
final List<String> deutanList = [
"1",
"15",
"2",
"3",
"14",
"13",
"4",
"12",
"5",
"6",
"11",
"10",
"7",
"9",
"8"
];
final List<String> tritanList = [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"15",
"8",
"14",
"9",
"13",
"10",
"11",
"12"
];
// transform your list-based filter to list of objects based for easy filtering
final List allFilterList = [
{"name": "normal", "list": normalList},
{"name": "protan", "list": protanList},
{"name": "deutan", "list": deutanList},
{"name": "tritan", "list": tritanList},
];
// result output
String result = "No such list!";
for (Map list in allFilterList) {
// filter using `any` Function from dart `List` class
bool filter = opaqueBoxes.any((item) => list["list"].contains(item));
if (filter) result = list["name"];
}
print("result: $result"); // result: tritan
}

最新更新