【发布时间】:2016-12-06 20:21:00
【问题描述】:
以下是在我的列表中可以找到的最大对象集的示例,该列表是在其他地方生成的。每组中可能有更少的组或更少的值。
CustomObject COOLING_111; //Start of Cooling group 1 - section 1
CustomObject COOLING_112;
CustomObject COOLING_113;
CustomObject COOLING_114;
CustomObject COOLING_115;
CustomObject COOLING_116;
CustomObject COOLING_117;
CustomObject COOLING_118;
CustomObject COOLING_121; //Start of Cooling group 1 - section 2
...
CustomObject COOLING_128
CustomObject COOLING_211; //Start of Cooling group 2 - section 1
...
CustomObject COOLING_218;
CustomObject COOLING_221; //Start of Cooling group 2 - section 2
...
CustomObject COOLING_228;
CustomObject COOLING_311; //Start of Cooling group 3 - section 1
...
CustomObject COOLING_318;
CustomObject COOLING_321; //Start of Cooling group 3 - section 2
...
CustomObject COOLING_328;
CustomObject COOLING_411; //Start of Cooling group 4 - section 1
...
CustomObject COOLING_418;
CustomObject COOLING_421; //Start of Cooling group 4 - section 2
...
CustomObject COOLING_428;
我如何编辑/创建一个循环或条件语句序列,以便我的数组中的变量按照示例描述的顺序专门分配:
- 将每个对象的值设置为 -1。
- 按以下模式设置任意数量的对象(例如前 6 个)的值:
- 每组的第一个对象(第 1 节)
- 然后是每组的第一个对象(第 2 节)
- 然后回到每组的第二个对象(第 1 部分)
- 最后是每组的第二个对象(第 2 节)
- 例如111 -> 211 -> 311 -> 411 -> 121 -> 221 -> 321 -> 421 -> 112 -> ... -> 122 -> 等等
目前,我正在创建一个值数组,无论 CustomObjects 冷却列表的大小如何,它们都应按分配顺序进行分配。冷却列表中的对象是无序的,只能通过解析名称中的索引来区分。如果示例大小中的数组实际上是 6,那么您将在按照上面的示例分配 221 后停止。
int count = 0;
Boolean init1 = false;
Boolean init2 = false;
Boolean init3 = false;
Boolean init4 = false;
values = new int[6] {12, 18, 9, 56, 112, 187} //Simplified but normally some code is abstracted and this array comes from another part of my code
do{
foreach (CustomObject obj in objList) {
obj.value = -1;
if(count < values.Length) {
string name1 = obj.Name.substring(8);
if (name1.StartWith("1")) {
if (!init1) {
obj.Value = values[count++];
init1 = true;
}
}
if (name1.StartsWith("2")) {
if (!init2) {
obj.Value = values[count++];
init2 = true;
}
}
if (name1.StartsWith("3")) {
if (!init3) {
obj.Value = values[count++];
init3 = true;
}
}
if (name1.StartsWith("4")) {
if (!init4) {
obj.Value = values[count++];
init4 = true;
break;
}
}
if ((count % 4 == 0) && (count > 0) && (count < values.Length)) {
init1 = false;
init2 = false;
init3 = false;
init4 = false;
}
if (count == values.Length) {
break;
}
}
}
}while (count < values.Length);
【问题讨论】:
-
您的单一问题是什么?我们不做跟随类型的问题。我们回答不仅是为了为您服务,而且最重要的是帮助未来的访客。只需为您遇到并需要帮助的一个问题创建一个minimal reproducible example。
-
@rene 我如何创建一个循环或条件语句序列,以便我的数组中的变量按照示例描述的顺序专门分配:111 -> 211 -> 311 -> 411 - > 121 -> 221 -> 321 -> 421 -> 112 -> ... -> 122 -> 等等。
-
@rene 我最大的问题是我无法理解如何有效地验证我是否应该在每次进行 foreach 迭代时从我的数组中分配下一个值,同时避免分配所有的值一次到第一部分中的前 6 个(例如)元素。创建更多标志显然不是一个好的解决方案。
标签: c# loops variable-assignment