C++ 为循环C+中断a+;
我试图在嵌套的if语句中打破for循环。。所以基本上我在做智囊团游戏,我想知道用户到底有多正确(讨论位置)。。所以基本上我想到了将人工智能的二进制数存储在一个数组中,然后将每个用户的二进制数与之进行比较。。一旦用户的一个二进制数字等于人工智能的一个二进制数字,它就应该跳出for循环。。。我这样想,我做到了:C++ 为循环C+中断a+;,c++,algorithm,C++,Algorithm,我试图在嵌套的if语句中打破for循环。。所以基本上我在做智囊团游戏,我想知道用户到底有多正确(讨论位置)。。所以基本上我想到了将人工智能的二进制数存储在一个数组中,然后将每个用户的二进制数与之进行比较。。一旦用户的一个二进制数字等于人工智能的一个二进制数字,它就应该跳出for循环。。。我这样想,我做到了: void MasterMind::evaluateCorrection() { // AI : 1 1 1 0 //USER: 1 0 1 1 //Sto
void MasterMind::evaluateCorrection()
{
// AI : 1 1 1 0
//USER: 1 0 1 1
//Store AI In Array
int AI[3];
int count = 0;
std::copy(binaries.begin(), binaries.end(), AI);
for(std::vector<char>::iterator itAI= numbers.begin() ; itAI != numbers.end(); itAI++)
{
for(int i=0; i<=3;i++)
{
char numberAt = *itAI;
int intNumberAt = numberAt - '0';
if(intNumberAt = AI[i])
{
cout << intNumberAt << " VS " << AI[i] << endl;
actuallyCorrect++;
break;
}
}
}
cout << "\n ACTUALLY CORRECT " << actuallyCorrect << endl;
}
那显然是不对的。。我输入了1123,它只是说4实际上是正确的。。。实际上只有2个是对的,1和1。请帮忙
超出范围,因此当AI[3]
时,您不能访问i=3
,数组的大小应该增加AI[i]
是一个赋值。使用intNumberAt=AI[i]
运算符执行相等性检查==
void MasterMind::evaluateCorrection()
{
// AI : 1 1 1 0
//USER: 1 0 1 1
//Store AI In Array
int AI[4] = {0}; // initialize for in case what is copied has insufficient number of elements
int count = 0;
std::copy(binaries.begin(), binaries.end(), AI);
for(std::vector<char>::iterator itAI= numbers.begin() ; itAI != numbers.end(); itAI++)
{
for(int i=0; i<=3;i++)
{
char numberAt = *itAI;
int intNumberAt = numberAt - '0';
if(intNumberAt == AI[i])
{
cout << intNumberAt << " VS " << AI[i] << endl;
actuallyCorrect++;
break;
}
}
}
cout << "\n ACTUALLY CORRECT " << actuallyCorrect << endl;
}
void策划人::evaluateCorrection()
{
//AI:10
//用户:1011
//将AI存储在数组中
int AI[4]={0};//如果复制的元素数量不足,则初始化
整数计数=0;
std::copy(binaries.begin(),binaries.end(),AI);
对于(std::vector::迭代器itAI=numbers.begin();itAI!=numbers.end();itAI++)
{
for(int i=0;iAI[3]
超出范围,因此当i=3
时,您不能访问AI[i]
intNumberAt=AI[i]
是一项任务,而不是比较。你确定这就是你想要做的吗?哇,那里@MikeCAT Woww…我做了goofOk工作@MikeCAT谢谢你的帮助,我很乐意接受你的回答。也许op想要AI[4]。关于if(intNumberAt=AI[i]的观察很好)
。我最近读到了1993年困扰Sunsoft的一个priority 1错误。经过一些密集的调试会话后,他们追踪到一条读为x==2
的语句。程序员的手指在equal键上弹跳,不小心按了两次而不是一次。在这里,op可能有一个equals
键允许按键。;)
void MasterMind::evaluateCorrection()
{
// AI : 1 1 1 0
//USER: 1 0 1 1
//Store AI In Array
int AI[4] = {0}; // initialize for in case what is copied has insufficient number of elements
int count = 0;
std::copy(binaries.begin(), binaries.end(), AI);
for(std::vector<char>::iterator itAI= numbers.begin() ; itAI != numbers.end(); itAI++)
{
for(int i=0; i<=3;i++)
{
char numberAt = *itAI;
int intNumberAt = numberAt - '0';
if(intNumberAt == AI[i])
{
cout << intNumberAt << " VS " << AI[i] << endl;
actuallyCorrect++;
break;
}
}
}
cout << "\n ACTUALLY CORRECT " << actuallyCorrect << endl;
}