C++ 简单地图绘制问题
我编写了一个简单的地图绘制程序,但有一些错误,我无法识别C++ 简单地图绘制问题,c++,C++,我编写了一个简单的地图绘制程序,但有一些错误,我无法识别 该错误仅在X坐标为正值时出现,在X坐标为负值时为ok 当我的范围只有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 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):
有人能帮我找出代码中的两个问题吗?谢谢。如果(currentY==testX和currentX==testY)
这看起来不对。你不应该把X和X,Y和Y进行比较吗
仔细一看,这一切都更加奇怪。外部循环生成行,但使用x
索引行。内部循环为每行生成列,并使用y
对其进行索引。对于哪个轴是X轴,哪个轴是Y轴,人们普遍感到困惑
编辑:啊,我现在看到问题了。当
currentY Y==0
时,您打印轴的数字,也打印点。问题是,当您打印Y轴时,您仍然打印一个点,因此Y轴右侧的所有内容都会移动1。您应该有另一个else
:
if( currentY == 0 )
{
....
}
else if (currentX == 0) // <--- add an else there
{
....
}
else if ...
if(当前y==0)
{
....
}
否则,如果(currentX==0)//比较currentY==testX和currentX==testY
,这是一个混淆还是有意的?这里实际上有两个相互抵消的错误:一个是您在比较中指出的错误,另一个是for
循环的是反向的,因此,x
和y
的角色将颠倒。您好,这是正确的,您可以在处看到结果,抱歉命名混乱。因为对于y,它从0开始,因此-6将导致它从-6开始而不是-5,并在4结束而不是5
. . . . . 5 . . . . . .
. . . * . 4 . . . . . .
. . . . . 3 . . . . . .
. . . . . 2 . . . . . .
. . . . . 1 . . . . . .
-5 -4 -3 -2 -1 0 1 2 3 4 5
. . . . . -1 . . . . . .
. . . . . -2 . . . . . .
. . . . . -3 . . . . . .
. . . . . -4 . . . . . .
. . . . . -5 . . . . . .
if( currentY == 0 )
{
....
}
else if (currentX == 0) // <--- add an else there
{
....
}
else if ...