2013-10-13 53 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