Trying If statement
Trying If statement
此if语句不起作用。
有人能告诉我为什么下面的代码不起作用,并帮助我修复吗?
i=0;
while(1)
{
if(counter2 < storeanother[0].size())
break;
int j=0;
while(1)
{
if(j < handler)
break;
outputFile2 << storeanother[0][counter2] << " + " << storeanother[1][counter2] << " = " << z80[i].get(j) << endl;
counter2+=1;
j++;
}
i++;
}
outputFile2.close();
您反转条件
for (init; cond, post) { body;}
相当于
init;
while (true) {
if (!cond) { // and not simply cond
break;
}
body;
post;
}
甚至
init;
while (cond) {
body;
post;
}
所以
if(counter2 < storeanother[0].size())
break;
应该是
if (counter2 >= storeanother[0].size())
break;
并且对于另一个循环类似。
如果你用错误的条件破坏了while,你必须将其反转。例如:
if(j == handler)
break;
因为在for循环中,当条件为true时,只有循环运行在这两种情况下,条件都成立了,所以它只是脱离了循环
把它换成反面,效果会很好。
像这样:
i=0;
// for (int i = 0; counter2 < storeanother[0].size(); i++)
while(1)
{
if(counter2 >= storeanother[0].size())
break;
int j=0;
//for (j = 0; j < handler; j++)
while(1)
{
if(j >= handler)
break;
outputFile2 << storeanother[0][counter2] << " + " << storeanother[1][counter2] << " = " << z80[i].get(j) << endl;
counter2+=1;
j++;
}
i++;
}
outputFile2.close();
除了其他人提到的if条件错误之外,我看不出有任何理由使用
while(1)
{
if(j >= handler)
break;
// do the stuff
而不是
while(j < handler)
{
// do the stuff
我的猜测是你正试图这样做:
int i=0;
while(counter2 < storeanother[0].size())
{
for (int j = 0; j < handler; j++)
{
outputFile2 << storeanother[0][counter2] << " + " <<streanother[1][counter2] << " = " << z80[i].get(j) << endl;
counter2+=1;
}
i++;
}
outputFile2.close();
第二部分:
i=0;
// for (int i = 0; counter2 < storeanother[0].size(); i++)
while(counter2 < storeanother[0].size()))
{
int j=0;
//for (j = 0; j < handler; j++)
while(j < handler)
{
outputFile2 << storeanother[0][counter2] << " + " << storeanother[1][counter2] << " = " << z80[i].get(j) << endl;
counter2+=1;
j++;
}
i++;
}
outputFile2.close();
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- std::bind c++ in if statement
- Arduino array and if statement
- C++ if statement with ||
- 在if-Statement条件下与分配有关的错误
- 为什么IF-Statement不返回C 中的值
- Problems in using ?: as if statement in C++
- C++ : "if statement"中的 prim()
- c if-statement的最大值在数组中的最大值未指向正确的元素
- 无法在循环中的if statement结束时增加
- 用if-statement移动构造函数,但使用三元运算符复制构造函数
- if statement中的平等比较
- C++ if statement
- 为什么"constructor-way"在"for-loop"允许但不允许"if-statement"声明变量?
- C++入门第5版练习1.19 "The If Statement"的误解 ?
- "complicated-nested" "for/if statement"真的遵循C标准吗?
- C++ if statement
- Trying If statement
- 是否可以在继续使用 c++ 之前将"for loop"嵌入"if statement"中以比较多个条件
- C++ if...then statement