在获得第二个if语句并获取新的char值之后,int number似乎不会被替换。有关如何更改此内容的任何提示,之后if语句将char值返回到int number?
if (c < '9' || c > '0')
{
int number = c - '0';
if (board[number - 1] == 'X' || board[number - 1] == 'O')
{
cout << "Please enter another Number";
cin.get(c);
cin.ignore(numeric_limits<int>::max(), '\n');
}
if (board[number - 1] != 'X' || board[number - 1] != 'O' )
{
if (player == 'X')
{
player = 'O';
}
else
{
player = 'X';
}
cout << "player " << player << " your number is " << number << endl;
// Your implementation here...
board[number - 1] = player;
}
}
这种情况一直在发生:
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number: 1
player X your number is 1
X | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number: 1
Please enter another Number2
player O your number is 1
O | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number:
答案 0 :(得分:4)
逻辑表达式c < '9' || c > '0'
和board[number - 1] != 'X' || board[number - 1] != 'O'
始终为真。他们是重言式。这可能意味着它们没有实现您打算实现的目标。你必须检查这些表达式。原样,你的代码没什么意义。
答案 1 :(得分:1)
变量&#39;数字&#39;没有被替换,因为你没有在&#39; if&#39;之后分配任何内容。声明。你需要重新分配给#number;#39;从cin读了另一个角色。事实上,它可能是最好的,如果不是一个,如果检查看板中的那个位置是否空闲,你循环直到用户进入一个未填充的方块:
while (board[number - 1] == 'X' || board[number - 1] == 'O') // loop until valid
{
cout << "Please enter another Number";
cin.get(c);
cin.ignore(numeric_limits<int>::max(), '\n');
number = c - '0'; // update array index
}