我編寫了一個簡單的地圖繪圖程序,但有一些我無法識別的錯誤。簡單的地圖繪製問題
- 該錯誤只發生在X座標爲正數時,其正數爲負數時出現。
- 當我的範圍僅爲11時,爲什麼會打印最後一列點?
下面的代碼:
int xRange = 11;
int yRange = 11;
string _space = " ";
string _star = " * ";
for(int x = xRange; x > 0; x--)
{
for(int y = 0; y < yRange; y++)
{
int currentX = x - 6;
int currentY = y - 5;
//demo input
int testX = 2; //<----------ERROR for +ve int, correct for -ve
int testY = -4; //<-------- Y is working ok for +ve and -ve int
//Print x-axis
if(currentY == 0)
{
if(currentX < 0)
cout << currentX << " ";
else
cout << " " << currentX << " ";
}
//Print y-axis
if(currentX == 0)
{
if(currentY < 0)
cout << currentY << " ";
else
//0 printed in x axis already
if(currentY != 0)
cout << " " << currentY << " ";
}
else if(currentY == testX and currentX == testY)
cout << _star;
else
cout << " . ";
}
//print new line every completed row print
cout << endl;
}
的輸出中爲演示輸入(X:2,Y:-4):(在從圖3示出了X這是錯誤的)
. . . . . 5 . . . . . .
. . . . . 4 . . . . . .
. . . . . 3 . . . . . .
. . . . . 2 . . . . . .
. . . . . 1 . . . . . .
-5 -4 -3 -2 -1 0 1 2 3 4 5
. . . . . -1 . . . . . .
. . . . . -2 . . . . . .
. . . . . -3 . . . . . .
. . . . . -4 . . * . . .
. . . . . -5 . . . . . .
爲演示輸入輸出(:-2,Y:×4):
. . . . . 5 . . . . . .
. . . * . 4 . . . . . .
. . . . . 3 . . . . . .
. . . . . 2 . . . . . .
. . . . . 1 . . . . . .
-5 -4 -3 -2 -1 0 1 2 3 4 5
. . . . . -1 . . . . . .
. . . . . -2 . . . . . .
. . . . . -3 . . . . . .
. . . . . -4 . . . . . .
. . . . . -5 . . . . . .
誰能幫助識別這兩個proble米在我的代碼?謝謝。
你比較'currentY == testX和currentX == testY',這是一個混合還是打算? – Kninnug