gpt4 book ai didi

c++ - 我一直收到错误 "‘else’ 而没有之前的 ‘if’“并且不知道为什么

转载 作者:行者123 更新时间:2023-12-05 08:28:48 27 4
gpt4 key购买 nike

几天前我开始使用 C++,每当我写一个 else if 语句时,我总是得到错误 ‘else’ without a previous ‘if’。这是我的代码(顺便说一句,我正在尝试在用户之间制作剪刀石头布。我最终想从生活大爆炸中添加蜥蜴和斯波克。):

/*
This program will play rock paper scissors with the user
*/


#include <iostream>
#include <stdlib.h>

int main() {


srand(time(NULL));


int computer = rand() % 3 + 1;


int user = 0;

std::cout << "===========================\nrock paper scissors!\n===========================\n";
std::cout << "1) ROCK\n";
std::cout << "2) PAPER\n";
std::cout << "3) SCISSORS\n";

std::cout << "shoot! \n";
std::cin >> user;
/*
if (user == 1) {
std::cout << "You pick rock!\n";
}
else if (user == 2) {
std::cout << "You pick paper!\n";
}
else if (user == 3) {
std::cout << "You picked scissors!\n";
}
else {
std::cout << "Invalid input, please select a number between 1 and 3\n";
*/
if (computer == 1)
std::cout << "CPU picks Rock!";
else if (computer == 2)
std::cout << "CPU picks Paper!";
else
std::cout << "CPU picks Scissors!";
if (user == 1)
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 2)
std::cout << "You pick paper!\n";
if (computer == 2)
std::cout << "You both tie!\n";
else if (computer == 3)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 3)
std::cout << "You picked scissors!\n";
if (computer == 3)
std::cout << "You both tie!\n";
else if (computer == 1)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else
std::cout << "Invalid input, please select a number between 1 and 3 (No Spaces please!)\n";
}






我尝试添加和删除大括号。 (虽然我听说它们是可选的)我尝试将 else if 切换到 if else 因为我总是忘记使用哪个。我还尝试检查以确保我没有使用任何分号,因为这似乎总是一个大问题。不过,我仍在学习,而且我对 C++ 还很陌生,所以请放轻松。

最佳答案

您的代码中的一些有问题的行:

if (user == 1)
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 2)

现在使用意图来说明这是如何解析的:

if (user == 1)
std::cout << "You pick rock!\n";
if (computer == 1)
std::cout << "You both tie!\n";
else if (computer == 2)
std::cout << "You lose. You really just aren't good at rock paper scissors lizard spock\n";
else
std::cout << "You win!\n";
else if (user == 2) // <-------------------------

只有一个语句时括号是可选的,例如

if (foo) {
std::cout << blabla;
}

// same as

if (foo) std::cout << blabla;

但是一旦有更多,括号就会有所不同。或者换句话说,下面两个是等价的:

if (foo) 
std::cout << blabla;
std::cout << whoops;

// is the same as

if (foo) {
std::cout << blabla;
}
std::cout << whoops;

最好总是写括号,即使在可以省略的情况下也是如此,否则,添加或删除一行需要重构整个代码块。

关于c++ - 我一直收到错误 "‘else’ 而没有之前的 ‘if’“并且不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74602815/

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