#include<iostream>
using namespace std;
void box();
int main()
{
cin.get();
box();
return 0;
}
int x,y;
char bn;
void box()
{
x=1,y=1;
for(;;)
{
cin >> 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 << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#& #" << endl;
cout << "#####" << endl;
}
else if(x==1,y==2)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "# & #" << endl;
cout << "#####" << endl;
}
else if(x==1,y==3)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "# &#" << endl;
cout << "#####" << endl;
}
else if(x==2,y==1)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "#& #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if(x==2,y==2)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# & #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if(x==2,y==3)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# &#" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if(x==3,y==1)
{
cout << "#####" << endl;
cout << "#& #" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if(x==3,y==2)
{
cout << "#####" << endl;
cout << "# & #" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if(x==3,y==3)
{
cout << "#####" << endl;
cout << "# &#" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
}
}
uj5u.com熱心網友回復:
if(x==1,y==2) 改成 if(x==1&&y==2)x==1,y==1是逗號運算式,運算式的值是最后的y==1的值,跟x就沒關系了,所以要用&&或||這樣的邏輯運算子。
uj5u.com熱心網友回復:
#include<iostream>
using namespace std;
void box();
int main()
{
//cin.get(); //這一行不需要,會導致程式運行后第一次輸入w\a\s\d,無法進行移動
box();
return 0;
}
int x, y;
char bn;
void box()
{
x = 1, y = 1;
for (;;)
{
cin >> bn;
if (bn == 'w') x++;
else if (bn == 's')x--;
else if (bn == 'a')y--;
else if (bn == 'd')y++;
cout << "x = " << x << "\t" << "y = " << y << endl; //每次按方向鍵后,加列印x\y的值,便于你代碼除錯時查看
if (x == 1 && y == 1) //條件判斷要用&&,表示x\y的值同時滿足才執行里面的陳述句
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#& #" << endl;
cout << "#####" << endl;
}
else if (x == 1 && y == 2)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "# & #" << endl;
cout << "#####" << endl;
}
else if (x == 1 && y == 3)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "# &#" << endl;
cout << "#####" << endl;
}
else if (x == 2 && y == 1)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "#& #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 2 && y == 2)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# & #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 2 && y == 3)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# &#" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 3 && y == 1)
{
cout << "#####" << endl;
cout << "#& #" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 3 && y == 2)
{
cout << "#####" << endl;
cout << "# & #" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 3 && y == 3)
{
cout << "#####" << endl;
cout << "# &#" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
}
}

深夜回復,如果滿意望給分,謝謝!
uj5u.com熱心網友回復:
#include<iostream>
using namespace std;
void box();
int main()
{
//cin.get(); 這一行不需要,會導致程式運行后第一次輸入w\a\s\d,無法進行移動
box();
return 0;
}
int x, y;
char bn;
void box()
{
x = 1, y = 1;
for (;;)
{
cin >> bn;
if (bn == 'w') x++;
else if (bn == 's')x--;
else if (bn == 'a')y--;
else if (bn == 'd')y++;
cout << "x = " << x << "\t" << "y = " << y << endl; //每次按方向鍵后,加列印x\y的值,便于你代碼除錯時查看
if (x == 1 && y == 1) ///條件判斷要用邏輯&&,不然y的值就沒有參與判斷了
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#& #" << endl;
cout << "#####" << endl;
}
else if (x == 1 && y == 2)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "# & #" << endl;
cout << "#####" << endl;
}
else if (x == 1 && y == 3)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "# &#" << endl;
cout << "#####" << endl;
}
else if (x == 2 && y == 1)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "#& #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 2 && y == 2)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# & #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 2 && y == 3)
{
cout << "#####" << endl;
cout << "# #" << endl;
cout << "# &#" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 3 && y == 1)
{
cout << "#####" << endl;
cout << "#& #" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 3 && y == 2)
{
cout << "#####" << endl;
cout << "# & #" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
else if (x == 3 && y == 3)
{
cout << "#####" << endl;
cout << "# &#" << endl;
cout << "# #" << endl;
cout << "# #" << endl;
cout << "#####" << endl;
}
}
}
上面的Code沒調好,重新發一次
有兩個問題:
1、Main()中的cin.get()會導致程式運行后第一次輸入w\a\s\d,無法進行移動
2、條件判斷x、y值要用邏輯&&運算子,你用逗號的話y的值沒有參與判斷
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/183404.html
標籤:C++ 語言
