C++ 为什么我在 for 循环中收到“Else without previous if”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14995454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:52:42  来源:igfitidea点击:

Why am I getting an 'Else without previous if' error within a for loop?

c++syntaxif-statement

提问by stringgy

I'm new to C++ and have been staring at my (probably abysmal) code for a while and can't figure out what's off about it.

我是 C++ 的新手,并且一直盯着我的(可能是糟糕的)代码一段时间,但无法弄清楚它有什么问题。

I'm trying to loop through a few iterations of if and else statements and must be doing something grammatically incorrect - as it shows compiler errors of 'else without a previous if'

我正在尝试遍历 if 和 else 语句的几次迭代,并且必须做一些语法错误的事情 - 因为它显示了“else without a previous if”的编译器错误

This is for a class and I'm trying to work it out, but if you see something obvious that I am overlooking I would love to know.

这是一个班级,我正在努力解决它,但是如果您看到我忽略的明显内容,我很想知道。

Thank you!

谢谢!

for (i = 0; i < iterationsNum; i++){
if (charlieAlive == 0) // Aarron's shot
        {
        if (aaronShot() == 1)
        charlieAlive = 1;
        }       
else (charlieAlive == 1 && bobAlive == 0);{         
        if (aaronShot() == 1)
        bobAlive = 1;
        }
else (charlieAlive == 1 && bobAlive == 1 && aaronAlive == 0);{
        cout << "Aaron is the Winner!\n";
        totalShot++;
        aaronCounter++;
        }
continue;



if (charlieAlive == 0 && aaronAlive ==0) // Bob's shot
        {
        if (bobShot() == 1) 
        charlieAlive = 1;
        }
else (charlieAlive == 1 && aaronAlive == 0);{
        if (bobShot() == 1)
        aaronAlive = 1;
        }
else (charlieAlive == 1 && aaronAlive == 1 && bobAlive == 0);{
        cout << "Bob is the Winner!\n";
        bobCounter++;
        totalShot++;
        }
continue;


if (charlieAlive == 0 && bobAlive == 0) // Charlie's shot   
        {
        bobAlive = 1;
        }
else (charlieAlive == 0 && bobAlive == 1 && aaronAlive == 0);{          
        aaronAlive = 1;
        totalShot++;
        }
else (charlieAlive == 0 && bobAlive == 1 && aaronAlive == 1);{
        cout << "Charlie is the Winner!\n";
        }
continue;

回答by Nawaz

elsedoesn' take any condition, but you've written this:

else不带任何条件,但你已经写了这个:

else (charlieAlive == 1 && bobAlive == 0);  //else : (notice semicolon)

which doesn't do what you intend it to do.

这不会做你打算做的事情。

You want to do thos:

你想这样做:

else if (charlieAlive == 1 && bobAlive == 0)  //else if : (semicolon removed)

Notice the difference.

注意区别。

Also, there can be at most one elseblock, associated with an ifblock Or a chain of if, else-if, else-ifblocks. That is, you can write this:

此外,最多可以有一个else区块,与一个if区块或一系列区块相关联if, else-if, else-if。也就是说,你可以这样写:

if (condition) {}
else {}

Or,

或者,

if (condition0) {}
else if (condition1) {}
else if (condition2) {}
else if (condition3) {}
else if (condition4) {}
else {}

In any case, elseblock is alwaysthe last block. After that if you write another elseblock, that would be an error.

在任何情况下,else始终是最后一个块。之后,如果您编写另一个else块,那将是一个错误。

Apart from that you also have a semicolon at wrong place. Fixed that also:

除此之外,您在错误的位置也有一个分号。还修复了:

else (charlieAlive == 1 && bobAlive == 0); <---- remove this semicolon!

Hope that helps.

希望有帮助。



Pick a good Introductory C++ Book. Here are few recommendations for all levels.

选择一本好的 C++ 入门书籍。以下是针对所有级别的一些建议。

回答by Krease

There are several problems I see here:

我在这里看到了几个问题:

  1. There are semicolons in your else statements - these aren't supposed to be there
  2. You have multiple else clauses for a single if. Use 'else if' when you are evaluating another condition - else is the catch-all for when no conditions are met
  3. I highly recommend proper indenting and consistent brace usage - not doing this isn't necessarily an error, but it will make it much easier to notice errors.
  1. 您的 else 语句中有分号 - 这些不应该在那里
  2. 单个 if 有多个 else 子句。在评估另一个条件时使用“else if” - else 是不满足任何条件时的包罗万象
  3. 我强烈推荐适当的缩进和一致的大括号使用 - 不这样做不一定是错误,但它会更容易发现错误。

回答by exexzian

you cant put condition statement in elsestatement

你不能把条件语句放在else语句中

correct for all elsestatements

所有其他语句都是正确的

like in else (charlieAlive == 1 && bobAlive == 0);

else (charlieAlive == 1 && bobAlive == 0);

elseis simply the alternative flow of if- i.e.

else只是替代流程if- 即

if(condition)  // if this fails go to else part
  {
--- // if condition true execute this
}
else{
 --- // will run when condition in if fails
}

so you don't have to put condition for else statement

所以你不必为 else 语句设置条件

Edit
where as else iftakes condition as well seems you wanted to do this
else if(your condition statements)// Note: no semicolon at the end

编辑
where aselse if需要条件似乎你想这样做
else if(your condition statements)// 注意:最后没有分号