c - 我可以在 C 中使用嵌套的 do while 循环吗?
问题描述
我的任务是为火车时刻表创建一个简单的程序。我需要让用户输入将运行多少列火车以及火车将在一天中运行多少次。对于每次运行,我需要一次告诉一列火车去,其他火车待命,直到他们都跑完为止。我正在尝试使用嵌套的 do while 循环来实现这一点,但运气不佳。这是可能的还是我从一开始就错了?
如果用户输入 2 列火车将运行 2 次,我的输出将显示为:Train 1 go。所有其他列车待命。火车2去。所有其他列车待命。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int numTrains = 0;
int timeBlocks = 0;
int numRuns = 1;
int timeBlocksran = 0;
printf("How many trains will be running today?\n");
scanf("%d", &numTrains);
printf("How many times will the trains run today?\n");
scanf("%d", &timeBlocks);
printf("OK. There will be %d trains running %d times today. Let's get them started. All Aboard.\n", numTrains, timeBlocks);
do
{
timeBlocksran++;
printf("Time Block %d\n", timeBlocksran);
do
{
printf("Train %d go. All other trains standby.\n", numRuns);
numRuns++;
} while (numRuns <= numTrains);
} while (timeBlocksran <= timeBlocks);
return 0;
}
如果用户输入 2 列火车将运行 2 次,我希望我的输出为:TIME BLOCK 1 Train 1 go。所有其他列车待命。火车2去。所有其他列车待命。时间块 2 火车 1 去。所有其他列车待命。火车2去。所有其他列车待命。
解决方案
推荐阅读
- search - 在文件中查找非 ASCII 文本
- javascript - 在 SparkAR 3D 中,自定义对象不能作为 AR 库 3D 对象在相同代码上正常工作
- sql - 使用 IMP (12.2.0.1.0) 时收到 IMP-00403 警告,但生成的失败的 import_sys.sql 为空
- python - 通过 ForeignKey 在 Django Shell 中出现“无属性”错误
- python-3.x - 所选列的逐行平均值不适用于我的数据框
- java - 从用户中选择一个多边形的算法
- java - Java 如何在后台管理同步?通过不挂起锁持有者线程,或者让它持有锁,即使被挂起?
- c# - 动态透视带有摘要的 SQL 数据表
- r - Understanding by-hand MLE logit estimation vs. optim
- jquery - 如何在不重定向的情况下从数据库实时更新赞成票和反对票数?