[英]C++ If statement error: expected ';' before '{' token
我写这个很有趣:
#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;
}
}
我试图在g ++中编译它,但出现了这个错误:
arsenalNorwich.cpp: In function, 'int main'
arsenalNorwich.cpp:15:30: error: expected ';' before '{' token
我不知道我做错了什么,我学校的CS导师也不知道。 尽管这只是为了娱乐,但它使我发疯。
if
您if
这里错过了一个:
else if (Norwich > Arsenal)
///^^^if missing
同时,放不好
int Arsenal = rand()%3;
int Norwich = rand()%3;
前main
。 还有一点是,您应该在调用rand()
之前首先设置随机种子。
您的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.