[英]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.