如何在 c++ 中进行比较和检查使用循环和数组
这是我新编辑的代码,我对存储在数组中的 X 和 O 的最终 if 循环遇到了麻烦。我正在尝试检查数组内是否已有 X 或 O。如果有一个,它会要求用户提供另一输入,然后重新开始,但是当我运行程序时,会发生这种情况:
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:
char c;
cout << "Please Enter Box Number: ";
cin >> c;
if (c > '9' || c < '0')
{
// error message
cout << "please enter a number 1-9" << " ";
cin >> c;
cin.ignore(numeric_limits<int>::max(), '\n');
}
if (c < '9' || c > '0')
{
int number = c - '0';
if (board[number - 1] == 'X' || board[number - 1] == 'O')
{
cout << "Please enter another Number";
cin >> c;
cin.ignore(numeric_limits<int>::max(), '\n');
}
else 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;
}
}
}
Here is my new edited code I am having trouble with my final if loop for the X and O's stored inside an array. I am trying to check if there is an X or O already inside the array or not. If there is one it asks the user for another input then start over again however when i run the program this happens:
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:
char c;
cout << "Please Enter Box Number: ";
cin >> c;
if (c > '9' || c < '0')
{
// error message
cout << "please enter a number 1-9" << " ";
cin >> c;
cin.ignore(numeric_limits<int>::max(), '\n');
}
if (c < '9' || c > '0')
{
int number = c - '0';
if (board[number - 1] == 'X' || board[number - 1] == 'O')
{
cout << "Please enter another Number";
cin >> c;
cin.ignore(numeric_limits<int>::max(), '\n');
}
else 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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先是第二个问题(最简单的):
Return 退出当前函数。因此,返回后,函数中不再执行任何指令。您应该删除 return 并将其余代码(如果输入无效则不应执行)放在该 if 的 else 块中。
第一个问题:
为什么要检查数组中每个位置的索引(如 board[7] = '8')。你已经知道了位置,你需要在每个单元格中标记出使用它的玩家;例如设置“X”(玩家 1)、“O”(玩家 2)、“ ”(空)的值。
如果将输入的“c”转换为整数并将其用作索引,而不是九个“if”,会更容易。使用 ASCII 字符(我不知道这是否仍然适用于 WCHAR、UTF-8 等),您可以使其变得快速而肮脏:
int k = c - '0';
board[k] = player;
编辑:
考虑到检查该正方形尚未使用:
First the second question (the easy one):
Return exits from the current function. So, after the return, no further instruction in your function is executed. You should remove the return and put the rest of the code (that should not be executed if the input is not valid) in the else block of that if.
First question:
Why do you check that the index of each position in the array (as in board[7] = '8'). You already know the position, what you need to mark in each cell is the player who uses it; for example set values of 'X' (player 1), 'O' (player 2), ' ' (empty).
Instead of nine "if", it will be easier if you convert your input 'c' into an integer and use it as an index. With ASCII characters (I do not know if this still workds with WCHAR, UTF-8 and so on) you could make it quick and dirt:
int k = c - '0';
board[k] = player;
Edit:
Taking into account checking that the square is not already used:
要循环返回直到输入有效值,您可以执行以下操作:
我不太确定您对另一部分的要求是什么。如果/当我这样做时,我会编辑我的回复。
To loop back until a valid value has been entered, you can do something like this:
I am not too sure what you are asking about the other part. I will edit my response if/when I do.
http://www.asciitable.com/
如果您查看 ascii 图表,您会发现所有这些字符具有十进制值。因此,当您的老师从用户输入的内容中减去“0”时,请说“1”,他实际上在做:
然后可以使用 [ ] 表示法直接访问您的数组,因此:
http://www.asciitable.com/
if you look at the ascii chart, you will notice that all of the characters have a decimal value to them. So when your teacher subtracts '0' from what the user entered, say '1' he is really doing:
your array can then be accessed directly using [ ] notation so: