-2

我不知道我的错误在哪里。看来我忘记了最基本的东西。

int xy [2];
cout << "Input X: ";
cin >> xy[0];
cout << "\nInput Y: ";
cin >> xy[1];
for ( int y = 0; y < 10; y++ )
{
    for ( int x = 0; x < 10; x++)
    {
        if ( x = xy[0] + 5 && y == xy[1] + 5)
        {
            cout << "°";
        }
        else
        {
            cout << "+";
        }
    }
    cout << "\n";
}
4

3 回答 3

3

可能你错过了这个:

if ( x == xy[0] + 5 && y == xy[1] + 5)
        ^ equality

这就是为什么你得到一个无限循环

=是赋值运算符,while==用于检查相等性

Puttingx= xy[0] + 5实际上将 x 分配给一个 value ,而不是比较因此永远不会结束内部循环

于 2013-09-03T20:10:41.027 回答
0

这是你想要的输出吗?

#include <iostream>

using namespace std;

int main()
{
  int xy [2];
  cout << "Input X: ";
  cin >> xy[0];
  cout << "\nInput Y: ";
  cin >> xy[1];
  for (int y = 0; y < 10; y++ )
  {
      for (int x = 0; x < 10; x++)
      {
        if (x == xy[0] + 5 && y == xy[1] + 5)
        {
            cout << "°";
        }
        else
        {
            cout << "+";
        }
      }
      cout << endl;
  }
}

执行程序.... $demo

Input X: 
Input Y: ++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
于 2013-09-03T20:12:15.220 回答
0

Maybe you should write this in line 10 (don't use the assignment operator):

if ( x == xy[0] + 5 && y == xy[1] + 5)
于 2013-09-03T20:13:13.657 回答