我收到以下错误
错误:与'operator&&'不匹配在'board [0] [0]&&板[0] [1]“
我的代码如下:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
string board[3][3];
board[0][0] = "[ ]";
board[0][1] = "[ ]";
board[0][2] = "[ ]";
board[1][0] = "[ ]";
board[1][1] = "[ ]";
board[1][2] = "[ ]";
board[2][0] = "[ ]";
board[2][1] = "[ ]";
board[2][2] = "[ ]";
string choice;
int counter;
do
{
for(counter=0;counter<5;counter++)
{
cout<<board[0][0]<<board[0][1]<<board[0][2]<<endl;
cout<<board[1][0]<<board[1][1]<<board[1][2]<<endl;
cout<<board[2][0]<<board[2][1]<<board[2][2]<<endl;
cout<<"(Player 1) Tell me the coordinates of where you want your X togo: ";
cin>>choice;
cout<<endl;
if(choice=="1,1")
{
board[0][0] = "[X]";
}
else if(choice=="1,2")
{
board[1][0] = "[X]";
}
else if(choice=="1,3")
{
board[2][0] = "[X]";
}
else if(choice=="2,1")
{
board[0][1] = "[X]";
}
else if(choice=="2,2")
{
board[1][1] = "[X]";
}
else if(choice=="2,3")
{
board[2][1] = "[X]";
}
else if(choice=="3,1")
{
board[0][2] = "[X]";
}
else if(choice=="3,2")
{
board[1][2] = "[X]";
}
else if(choice=="3,3")
{
board[2][2] = "[X]";
}
if(board[0][0] && board[0][1] && board[0][2] == "[X]" || board[1][0] && board[1][1] && board[1][2] == "[X]" || board[2][0] && board[2][1] && board[2][2] == "[X]" || board[0][0] && board[1][0] && board[2][0] == "[X]" || board[0][1] && board[1][1] && board[2][1] == "[X]" || board[0][2] && board[1][2] && board[2][2] == "[X]" || board[0][0] && board[1][1] && board[2][2] == "[X]" ||board[2][0] && board[1][1] && board[0][2] == "[X]" ||)
{
cout<<"Player 1 wins!"<<endl;
}
}
}
while(counter<5);
return 0;
}
错误所在的行如下:
if(board[0][0] && board[0][1] && board[0][2] == "[X]" || board[1][0] && board[1][1] && board[1][2] == "[X]" || board[2][0] && board[2][1] && board[2][2] == "[X]" || board[0][0] && board[1][0] && board[2][0] == "[X]" || board[0][1] && board[1][1] && board[2][1] == "[X]" || board[0][2] && board[1][2] && board[2][2] == "[X]" || board[0][0] && board[1][1] && board[2][2] == "[X]" ||board[2][0] && board[1][1] && board[0][2] == "[X]" ||)
答案 0 :(得分:10)
您需要更改以下内容:
board[0][0] && board[0][1] && board[0][2] == "[X]"
要:
board[0][0] == "[X]" && board[0][1] == "[X]" && board[0][2] == "[X]"
以便&amp;&amp;和/或是布尔值
答案 1 :(得分:1)
我建议使用字符数组而不是字符串数组 - 然后,当你要求用户输入并输入说“1,3”时 - 你可以简单地声明两个整数并使用它们:
int x, y;
scanf("%i,%i", &x, &y);
board[x][y] = 'X';
// and when printing the board, use: cout << "["<<board[x][y]<<"]";
// - or better yet, use printf :)
// alternatively with cin:
int x, y;
cin >> x;
cin.ignore(1,',');
cin >> y;
board[x][y] = 'X';
这会缩短所有“确定用户选择的方块”:)