using namespace std;
Void box ();
Int main ()
{
Cin. The get ();
The box ();
return 0;
}
int x,y;
Char bn;
Void box ()
{
X=1, y=1;
For (;; )
{
Cin & gt;> Bn;
If (bn=='w') x++;
Else if (bn=='s') x -;
Else if (bn=='a') - y;
Else if (bn=='d') y++;
If (x==1, y==1)
{
cout <"# # # # #" & lt;
Else if (x==1, y==2)
{
cout <"# # # # #" & lt;
Else if (x==1, y==3)
{
cout <"# # # # #" & lt;
Else if (x==2, y==1)
{
cout <"# # # # #" & lt;
Else if (x==2, y==2)
{
cout <"# # # # #" & lt;
Else if (x==2, y==3)
{
cout <"# # # # #" & lt;
Else if (x==3, y==1)
{
cout <"# # # # #" & lt;
Else if (x==3, y==2)
{
cout <"# # # # #" & lt;
Else if (x==3, y==3)
{
cout <"# # # # #" & lt;
}
}
CodePudding user response:
If (x==1, y==2) into the if (x==1 & amp; & Y==2)X==1, y==1 is a comma expression, the value of the expression is the final value of y==1, with x it doesn't matter, so want to use & amp; & Or | | this logical operators,
CodePudding user response:
# include
using namespace std;
Void box ();
Int main ()
{
//cin. The get (); //this line does not need to, will cause the program after the operation for the first time input w \ a \ \ s d, unable to move
The box ();
return 0;
}
Int x, y;
Char bn;
Void box ()
{
X=1, y=1;
For (;; )
{
Cin & gt;> Bn;
If (bn=='w') x++;
Else if (bn=='s') x -;
Else if (bn=='a') - y;
Else if (bn=='d') y++;
cout <"X=" & lt;//every time after press the direction key, and print the value of x, y, facilitate when you debug the code to see
If (x==1 & amp; & y==1) //condition judgment to use & amp; & , said the value of x, y and satisfy to execute the statements inside the
{
cout <"# # # # #" & lt;cout <& lt; "# #" & lt; cout <"# #" & lt; cout <"# & amp; # "& lt; cout <"# # # # #" & lt; }
Else if (x==1 & amp; & Y==2)
{
cout <"# # # # #" & lt;cout <"# #" & lt; cout <"# #" & lt; cout <"# & amp; # "& lt; cout <"# # # # #" & lt; }
Else if (x==1 & amp; & Y==3)
{
cout <"# # # # #" & lt;cout <"# #" & lt; cout <"# #" & lt; cout <"# & amp; # "& lt; cout <"# # # # #" & lt; }
Else if (x==2 & amp; & Y==1)
{
cout <"# # # # #" & lt;cout <"# #" & lt; cout <"# & amp; # "& lt; cout <"# #" & lt; cout <"# # # # #" & lt; }
Else if (x==2 & amp; & Y==2)
{
cout <"# # # # #" & lt;cout <"# #" & lt; cout <"# & amp; # "& lt; cout <"# #" & lt; cout <"# # # # #" & lt; }
Else if (x==2 & amp; & Y==3)
{
cout <"# # # # #" & lt;cout <"# #" & lt; cout <"# & amp; # "& lt; cout <"# #" & lt; cout