C++ 检查二维数组中的无效值
有没有办法为第一列设置1-13限制,为第二列设置1-4限制 我的输出如下所示: 您选择的卡值和套装为: 1 2 34 5.6 7 8 910 这是到目前为止我的代码,我知道while循环会影响2d数组中的所有值。我被困在如何设置两个不同的限制,因为我的输入现在只接受1-13之间的值C++ 检查二维数组中的无效值,c++,visual-c++,C++,Visual C++,有没有办法为第一列设置1-13限制,为第二列设置1-4限制 我的输出如下所示: 您选择的卡值和套装为: 1 2 34 5.6 7 8 910 这是到目前为止我的代码,我知道while循环会影响2d数组中的所有值。我被困在如何设置两个不同的限制,因为我的输入现在只接受1-13之间的值 void getCard(int card[][2]) { int i, j; printf("\nPlease enter the card value followed by its suit\n"); fo
void getCard(int card[][2])
{
int i, j;
printf("\nPlease enter the card value followed by its suit\n");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%i", &card[i][j]);
while (card[i][j] > 13|| card[i][j] < 1)
{
printf("\nOnly enter card value between 1 to 13.\n");
scanf("%i", &card[i][j]);
}
}
}
}
void main(void)
{
int i, j;
int card[5][2];
getCard(card);
printf("\nThe card value and suit that you've chosen are:\n");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 2; j++)
{
printf("%i ", card[i][j]);
if (j == 1)
{
printf("\n");
}
}
}
}
void getCard(int card[][2])
{
int i,j;
printf(“\n请输入卡值,后跟其套色”\n”);
对于(i=0;i<5;i++)
{
对于(j=0;j<2;j++)
{
scanf(“%i”、&card[i][j]);
while(card[i][j]>13 | | card[i][j]<1)
{
printf(“\n仅输入介于1到13之间的卡值。\n”);
scanf(“%i”、&card[i][j]);
}
}
}
}
真空总管(真空)
{
int i,j;
智能卡[5][2];
卡;
printf(“\n您选择的卡值和套装为:\n”);
对于(i=0;i<5;i++)
{
对于(j=0;j<2;j++)
{
printf(“%i”,卡片[i][j]);
如果(j==1)
{
printf(“\n”);
}
}
}
}
您可以执行以下操作:
for (j = 0; j < 2; j++)//loop by columns
{
int rest=13;//for the first column
if (j==1)
rest=4;//for the second column
for (i = 0; i < 5; i++)
{
scanf("%i", &card[i][j]);
while (card[i][j] > rest|| card[i][j] < 1)//the loop depends on the value of rest
{
printf("\nOnly enter card value between 1 to %i.\n",rest);
scanf("%i", &card[i][j]);
}
}
}
(j=0;j<2;j++)的for//按列循环
{
int rest=13;//对于第一列
如果(j==1)
rest=4;//用于第二列
对于(i=0;i<5;i++)
{
scanf(“%i”、&card[i][j]);
while(card[i][j]>rest | | card[i][j]<1)//循环取决于rest的值
{
printf(“\n仅输入介于1到%i之间的卡值。\n”,其余);
scanf(“%i”、&card[i][j]);
}
}
}
如果仍需要浏览这些行,请执行以下操作:
for (i = 0; i < 5; i++)//loop by rows
{
for (j = 0; j < 2; j++)
{
int rest=13;//for the first column
if (j==1)
rest=4;//for the second column
scanf("%i", &card[i][j]);
while (card[i][j] > rest|| card[i][j] < 1)//the loop depends on the value of rest
{
printf("\nOnly enter card value between 1 to %i.\n",rest);
scanf("%i", &card[i][j]);
}
}
}
(i=0;i<5;i++)的for//按行循环
{
对于(j=0;j<2;j++)
{
int rest=13;//对于第一列
如果(j==1)
rest=4;//用于第二列
scanf(“%i”、&card[i][j]);
while(card[i][j]>rest | | card[i][j]<1)//循环取决于rest的值
{
printf(“\n仅输入介于1到%i之间的卡值。\n”,其余);
scanf(“%i”、&card[i][j]);
}
}
}
请将输入、预期输出和实际输出作为文本输入问题本身。不是作为图像,不是作为链接,也绝对不是作为图像的链接。