gpt4 book ai didi

C++ If语句错误: expected ';' before '{' token

转载 作者:太空宇宙 更新时间:2023-11-03 10:31:16 25 4
gpt4 key购买 nike

我写这个是为了好玩:

#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:

  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;

关于C++ If语句错误: expected ';' before '{' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com