{ int safe_battery_level = 50;//設置安全電量 int charge_task_id;//設置充電任務的id int charge_task_priority;//設置充電任務的優先級 int max_task_id = 0; int max_charge_priority = 0; if (agvs[i].getBattery() < safe_battery_level) { bool has_charge_task = false; for (int k = 0; k < tasks.size(); k++) { if (tasks[k].a_id== agvs[i].id_ && tasks[k].Task_type == 1) { has_charge_task = true; break; } for (int j = 0; j < charges.size(); j++) { if (charges[j].charge_available == true) { charges[j].setavailable(false); for (int k = 0; k < tasks.size(); k++) { if (tasks[k].id > max_task_id) { max_task_id = tasks[k].id; } if (tasks[k].Task_type == 1 && tasks[k].priority > max_charge_priority) { max_charge_priority = tasks[k].priority; } } charge_task_id = max_task_id + 1; charge_task_priority = max_charge_priority + 1; Task charge_task(charge_task_id, agvs[i].current_x, agvs[i].current_y, charges[j].charge_x, charges[j].charge_y, charge_task_priority, 0, agvs[i].id_, 1); // 创建一个充电任务 tasks.push_back(charge_task); // 将充电任务添加到 tasks 容器中 break; } } } } },邏輯有問題怎麽修改
时间: 2024-02-14 19:05:10 浏览: 66
浅谈int8_t int64_t size_t ssize_t的相关问题(详解)
经过仔细分析,发现代码中的第二个 for 循环没有正确关闭,导致程序的逻辑出现了错误。修改后的代码如下:
```
int safe_battery_level = 50;
int charge_task_id;
int charge_task_priority;
int max_task_id = 0;
int max_charge_priority = 0;
if (agvs[i].getBattery() < safe_battery_level) {
bool has_charge_task = false;
for (int k = 0; k < tasks.size(); k++) {
if (tasks[k].a_id == agvs[i].id_ && tasks[k].Task_type == 1) {
has_charge_task = true;
break;
}
}
if (!has_charge_task) {
for (int j = 0; j < charges.size(); j++) {
if (charges[j].charge_available == true) {
charges[j].setavailable(false);
for (int k = 0; k < tasks.size(); k++) {
if (tasks[k].id > max_task_id) {
max_task_id = tasks[k].id;
}
if (tasks[k].Task_type == 1 && tasks[k].priority > max_charge_priority) {
max_charge_priority = tasks[k].priority;
}
}
charge_task_id = max_task_id + 1;
charge_task_priority = max_charge_priority + 1;
Task charge_task(charge_task_id, agvs[i].current_x, agvs[i].current_y, charges[j].charge_x, charges[j].charge_y, charge_task_priority, 0, agvs[i].id_, 1);
tasks.push_back(charge_task);
break;
}
}
}
}
```
修改后的代码将第二个 for 循环放在了 if (!has_charge_task) 的条件判断内,并且加入了花括号以正确地关闭循环。这样就可以避免出现逻辑错误了。
阅读全文