尝试If语句

时间:2015-12-17 09:44:10

标签: c++ visual-c++

这个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();

5 个答案:

答案 0 :(得分:2)

您反转条件

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;

和其他循环类似。

答案 1 :(得分:1)

你在错误的情况下打破了一段时间,你必须扭转它。例如:

if(j == handler)
    break;

答案 2 :(得分:1)

因为在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();

答案 3 :(得分:0)

除了其他人提到的if条件错误外,我认为没有理由

while(1)
{
    if(j >= handler)
        break;
    // do the stuff

而不是

while(j < handler)
{
    // do the stuff

答案 4 :(得分:0)

我的猜测是你试图这样做:

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();