我需要用 X 标记用户输入的座位,然后用标记的座位再次显示飞机布局(数组)并每次都重复。这是我的代码,我有点卡住了。如果可以的话,请帮我把它分成两个函数:第一个函数应该打印出数组,第二个函数应该接受用户输入并标记座位,然后调用前一个函数来刷新显示。
我的输出应该是这样的:
1 X B C D 2 A X C D 3 A B C D 4 A B X D 5 A B C D 6 A B C D 7 A B C D
我的代码是:
#include <iostream>
using namespace std;
int main ()
{
int rowNumber=-1, columnNumber=-1;
char columnLetter;
char airplane[7][4] = {{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'}};
for (int i=0; i<7; i++)
{
cout<<i+1<<"\t";
for (int j=0; j<4; j++)
{
cout<<airplane[i][j]<<"\t";
}
cout<<"\n";
}
do
{
cout<<"Enter row number:"<<endl;
cin>>rowNumber;
}while ((rowNumber<1)||(rowNumber>7));
do
{
cout<<"Enter column letter:"<<endl;
cin>>columnLetter;
}while (columnLetter!='A'&&columnLetter!='B'&&columnLetter!='C'&&columnLetter!='D');
if (columnLetter=='A')
{
columnNumber=0;
}
else if (columnLetter=='B')
{
columnNumber=1;
}
if (columnLetter=='C')
{
columnNumber=2;
}
else if (columnLetter=='D')
{
columnNumber=3;
}
airplane[rowNumber][columnNumber]='X';
system ("PAUSE");
return 0;
}
我试过了,但它没有编译,所以我不能移动到下一个打印标记座位的函数:
#include <iostream>
using namespace std;
void fillArray (char array[][4]);
int main ()
{
char airplane[7][4] = {{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'}};
fillArray (airplane[][4]);
system ("PAUSE");
return 0;
}
void fillArray (char array[][4])
{
for (int i=0; i<7; i++)
{
cout<<i+1<<"\t";
for (int j=0; j<4; j++)
{
cout<<array[i][j]<<"\t";
}
cout<<"\n";
}
return;
}
enter code here