【发布时间】:2014-04-06 23:08:07
【问题描述】:
我正在做我的课堂作业,但我被困在二维数组、嵌套循环和特定条件下。
void main (){
int a;
int b;
int orders[100][4];
char confirm[50];
//printf("Enter the number of boxes of whistles you need on this invoice");
for (a = 0; a <=100; a++){
for(b =0; b <4; b++){
printf("Enter the number of boxes of whistles you need on this invoice");
scanf("%d", &orders[a][b]);
while (orders[a][b] <0)
{
printf("Invalid value! Please check the number.");
scanf("%d", &orders[a][b]);
}
printf("Enter the number of boxes of bells you need on this invoice.");
scanf("%d", &orders[a][b]);
while (orders[a][b] <0)
{
printf("Invalid value! Please check the number.");
scanf("%d", &orders[a][b]);
}
printf("Enter the number of boxes of horns you need on this invoice");
scanf("%d", &orders[a][b]);
while (orders[a][b]<0)
{
printf("Invalid value! Please check the number.");
scanf("%d", &orders[a][b]);
}
printf("Do you want to enter more orders?");
scanf("%s", confirm);
if (strcmp(confirm, "N") == 1){
break;
}
}
如您所见,我应该用用户输入填充二维数组(如果您愿意的话,可以使用发票图表)。我不确定我的代码是否正确。最后,当用户说 N 表示“否”而程序询问“你想输入更多订单吗?”时,我试图跳出循环。当我尝试代码时,它会询问所有问题,并且似乎存储了它,但即使我把 N 放在最后,我也无法跳出循环。
所以...我的问题是
- 我存储的带有用户输入的二维数组的编码是否正确?
- 当我说 N(或任何其他字母或数字)时,如何退出循环(或结束循环)?
谢谢!
【问题讨论】:
标签: arrays store nested-loops break