C++中尝试多种循环均未成功,如何实现轮次交互直至双方初始时间归零?
双人轮流服务扣费循环实现方案
现有代码的核心错误
- 循环后误加分号:
while ((initialTime1 > 0));、while ((initialTime2 > 0));末尾的分号会把循环体变为空语句,后续大括号内的代码完全不会被循环执行 - 循环逻辑不符合轮流要求:现有代码是先后独立执行两个用户的循环,而非按轮次交替交互
- 缺少用户1的输入步骤:用户1的分支判断直接使用
groupNumber1,没有写cin语句让用户1输入组别编号 - 终止条件不符合需求:现有逻辑是单个用户时间为0就停止该用户的循环,而非两个用户时间全部为0才终止整个流程
适配场景的最优循环选择
该场景需要按轮次交替执行流程,直到两个用户的剩余时间都归零,最优选择是外层while循环:
- 不需要用for:循环次数完全由用户的组别选择决定,没有固定迭代上限,用for需要额外维护无意义的迭代变量,冗余度高
- 不需要用do-while:如果初始状态下两个用户的时间都为0,do-while会强制执行一次交互流程,不符合业务逻辑
- while的终止条件设为
(initialTime1 > 0 || initialTime2 > 0):只要任意一个用户还有剩余时间就继续运行,每轮内分别判断两个用户的剩余时间,有余额则执行对应交互、扣费、加钱操作,余额为0则自动跳过该用户,完全匹配轮流交互、两人都用完才终止的需求
修正后的实现代码
// 提前定义组别对应的时间、金额常量 const int teensTime = 20, teensMoney = 10; const int familyTime = 30, familyMoney = 20; const int businessmenTime = 25, businessmenMoney = 18; const int seniorcitizensTime = 15, seniorcitizensMoney = 8; const int youngprofessionalsTime = 22, youngprofessionalsMoney = 15; // 提前初始化两个用户的初始时间、收入、姓名等变量 int initialTime1 = 100, initialMoney1 = 0; int initialTime2 = 100, initialMoney2 = 0; string name1 = "User1", name2 = "User2"; int groupNumber1, groupNumber2; // 外层循环:任意一个用户有剩余时间就继续轮次 while (initialTime1 > 0 || initialTime2 > 0) { // 用户1流程:仅当剩余时间大于0时执行 if (initialTime1 > 0) { cout << name1 << ", it is your turn. Choose what group to serve. \nChoose wisely. \n" "1 - A group of teenagers \n" "2 - A family \n" "3 - A group of businessmen \n" "4 - A senior citizen couple \n" "5 - Young professional couple \n"; cout << "Your choice: "; cin >> groupNumber1; cout << '\n'; cout << "You chose group number " << groupNumber1 << "." << endl; cout << '\n'; if (groupNumber1 == 1) { initialMoney1 += teensMoney; initialTime1 -= teensTime; cout << "You now have a total of $" << initialMoney1 << " and have " << initialTime1 << " minutes remaining." << endl; } else if (groupNumber1 == 2) { initialMoney1 += familyMoney; initialTime1 -= familyTime; cout << "You now have a total of $" << initialMoney1 << " and have " << initialTime1 << " minutes remaining." << endl; } else if (groupNumber1 == 3) { initialMoney1 += businessmenMoney; initialTime1 -= businessmenTime; cout << "You now have a total of $" << initialMoney1 << " and have " << initialTime1 << " minutes remaining." << endl; } else if (groupNumber1 == 4) { initialMoney1 += seniorcitizensMoney; initialTime1 -= seniorcitizensTime; cout << "You now have a total of $" << initialMoney1 << " and have " << initialTime1 << " minutes remaining." << endl; } else if (groupNumber1 == 5) { initialMoney1 += youngprofessionalsMoney; initialTime1 -= youngprofessionalsTime; cout << "You now have a total of $" << initialMoney1 << " and have " << initialTime1 << " minutes remaining." << endl; } } sleep(4); system("CLS"); // 用户2流程:仅当剩余时间大于0时执行 if (initialTime2 > 0) { cout << name2 << ", it is your turn. Choose what group to serve. \nChoose wisely. \n" "1 - A group of teenagers \n" "2 - A family \n" "3 - A group of businessmen \n" "4 - A senior citizen couple \n" "5 - Young professional couple \n"; cout << "Your choice: "; cin >> groupNumber2; cout << '\n'; cout << "You chose group number " << groupNumber2 << "." << endl; cout << '\n'; if (groupNumber2 == 1) { initialMoney2 += teensMoney; initialTime2 -= teensTime; cout << "You now have a total of $" << initialMoney2 << " and have " << initialTime2 << " minutes remaining." << endl; } else if (groupNumber2 == 2) { initialMoney2 += familyMoney; initialTime2 -= familyTime; cout << "You now have a total of $" << initialMoney2 << " and have " << initialTime2 << " minutes remaining." << endl; } else if (groupNumber2 == 3) { initialMoney2 += businessmenMoney; initialTime2 -= businessmenTime; cout << "You now have a total of $" << initialMoney2 << " and have " << initialTime2 << " minutes remaining." << endl; } else if (groupNumber2 == 4) { initialMoney2 += seniorcitizensMoney; initialTime2 -= seniorcitizensTime; cout << "You now have a total of $" << initialMoney2 << " and have " << initialTime2 << " minutes remaining." << endl; } else if (groupNumber2 == 5) { initialMoney2 += youngprofessionalsMoney; initialTime2 -= youngprofessionalsTime; cout << "You now have a total of $" << initialMoney2 << " and have " << initialTime2 << " minutes remaining." << endl; } } sleep(4); system("CLS"); } // 所有时间耗尽后输出最终结果 cout << "All time used. Final earnings:" << endl; cout << name1 << ": $" << initialMoney1 << endl; cout << name2 << ": $" << initialMoney2 << endl;
内容的提问来源于stack exchange,提问作者claire
相关产品推荐
相关产品推荐

