我可以看到我的编码错误,有人可以帮助我吗? [英] i can see what i am doing wrong with my coding, can someone help me out?
本文介绍了我可以看到我的编码错误,有人可以帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void flip( int i)
{
断言(i == 1 && i == 0 );
if (i = 1 )
i = 0 跨度>;
else
i = 0 ;
}
int main()
{
int i = 1 ;
断言(i == 1 );
flip(i);
断言(i == 0 );
flip(i);
断言(i == 1 );
cout<< 成功!\ n;
return 0 ;
}
[HB:添加预标签]
解决方案
< blockquote> flip()中的断言将始终失败,因为您试图断言i等于0和1.无论输入值如何,您还将i(local)的值更改为零。
对flip()的调用不会改变main()中i的值。你需要翻转引用变量i而不是它的值的副本:即。 void flip(int * i)。
一些事情,我认为是错的
断言(i == 1 && i == 0 ); // 为什么&& ;.我认为这应该是||
如果(i = 1 )
i = 0 ;
else
i = 0 ; // 在这里你必须设置i = 1(假设这是翻转功能应该做的
编辑:if条件中的=应更改为==
void flip ( int i )
{
assert( i==1 && i==0);
if(i = 1)
i=0;
else
i=0;
}
int main()
{
int i = 1;
assert(i==1);
flip(i);
assert(i==0);
flip(i);
assert(i==1);
cout << "Success!\n";
return 0;
}
[HB: add pre tags]
解决方案
The assertion in flip() will always fail because you are trying to assert that i is equal to both 0 and 1. You also change the value of i (locally) to zero regardless of input value.
The call to flip() does not change the value of i in main(). You need to make flip refer to the variable i instead of a copy of its value: ie. "void flip(int* i)".
Couple of things, i think are wrong
assert( i==1 && i==0); // Why &&. I think this should be || if(i = 1) i=0; else i=0; // Here you will have to set i = 1 (assuming this is what flip function is supposed to do
EDIT: the "=" in your if conditions should be changed to "=="
这篇关于我可以看到我的编码错误,有人可以帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文