2013-10-13 57 views
1

我編寫了一個簡單的地圖繪圖程序,但有一些我無法識別的錯誤。簡單的地圖繪製問題

  1. 該錯誤只發生在X座標爲正數時,其正數爲負數時出現。
  2. 當我的範圍僅爲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米在我的代碼?謝謝。

+0

你比較'currentY == testX和currentX == testY',這是一個混合還是打算? – Kninnug

回答

2

if(currentY == testX and currentX == testY)

這看起來不正確的。你不應該把X與Y和Y比較嗎?

仔細一看,就更加陌生。您的外部循環會生成行,但您可以使用x將它們編入索引。內部循環爲每一行生成列,並使用y爲其編制索引。對於哪一個軸是X軸和哪個軸是Y軸存在一般混淆。

編輯:啊,我現在看到了問題。當currentY == 0時,您打印該軸的編號,並且也打印該點的

+0

實際上這裏有兩個錯誤會互相抵消:你指出的比較,以及'for'循環倒退的事實,以便'x'和'y'的角色變得相反。 – interjay

+0

嗨,它的正確,你可以看到http://ideone.com/WHVFOm的結果,抱歉命名混淆。 – curiosity

+0

,因爲對於y,它從0開始,因此-6將導致它從-6開始而不是-5開始,並且以4結束而不是5 – curiosity

1

的問題是,當你打印Y軸,你打印一個點,所以一切以y軸是由1偏移了正確的你應該有另一個else在那裏:

if(currentY == 0) 
{ 
    .... 
} 
else if (currentX == 0) // <--- add an else there 
{ 
    .... 
} 
else if ... 
+0

感謝解決方案=) – curiosity