[英]C++ If statement error: expected ';' before '{' token
I was writing this out for fun: 我写这个很有趣:
#include <iostream>
#include <cstdlib>
using namespace std;
int Arsenal = rand()%3;
int Norwich = rand()%3;
int main () {
if (Arsenal > Norwich) {
cout << "Arsenal win the three points, they are in the Top Four";
return 0;
} else if (Arsenal == Norwich) {
cout << "The game was a draw, both teams gained a point";
return 0;
} else (Norwich > Arsenal) {
cout << "Norwich won, Arsenal lost";
return 0;
}
}
and I tried to compile it in g++, yet I get this error: 我试图在g ++中编译它,但出现了这个错误:
arsenalNorwich.cpp: In function, 'int main'
arsenalNorwich.cpp:15:30: error: expected ';' before '{' token
I have no idea what I did wrong, and neither does the CS tutor at my school. 我不知道我做错了什么,我学校的CS导师也不知道。 Although it's just for fun, it's driving me crazy.
尽管这只是为了娱乐,但它使我发疯。
you missed one if
here: if
您if
这里错过了一个:
else if (Norwich > Arsenal)
///^^^if missing
Meanwhile, it is not good to put 同时,放不好
int Arsenal = rand()%3;
int Norwich = rand()%3;
before main
. 前
main
。 Another point is that you should first set random seed before calling rand()
. 还有一点是,您应该在调用
rand()
之前首先设置随机种子。
Your if-else
can be simplified as follows: 您的
if-else
可以简化如下:
if (Arsenal > Norwich) {
cout << "Arsenal win the three points, they are in the Top Four";
} else if (Arsenal == Norwich) {
cout << "The game was a draw, both teams gained a point";
} else { //^^^no need to compare values again since it must be Norwich > Arsenal
//when execution reaches this point
cout << "Norwich won, Arsenal lost";
}
return 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.