否则在新程序中没有以前的 if 错误
else without a previous if error in new program
我是C++新手,开始学习它只是为了好玩。 我正在使用Codecademy的C++课程 我在下面编写了程序,但无法编译它。我不断收到错误"否则没有以前的其他如果"......
#include <iostream>
int main() {
double earthw = 0;
double planet = 0;
double weight = 0;
//Ask and receive earth weight
std::cout << "What is your earth weight? n";
std::cin >> earthw;
//Ask which planet to figth on
std::cout << "Which Planet Would you like to fight in? n";
//List all planets
std::cout << "1 - Venusn";
std::cout << "2 - Marsn";
std::cout << "3 - Jupitern";
std::cout << "4 - Saturnn";
std::cout << "5 - Uranusn";
std::cout << "6 - Neptunen";
//Receive planet number
std::cin >> planet;
//Calculations and inputing weight on specific planet
if (planet == 1) {
weight = earthw * 0.78;
}
else if (planet == 2) {
weight = earthw * 0.39;
}
else if (planet == 3) {
weight = earthw * 2.65;
}
else if (planet == 4) {
weight = earthw * 1.17;
}
else if (planet == 5) {
weight = earthw * 1.05;
}
else if (planet == 6) {
weight = earthw * 1.23;
}
std::cout << "Your weight is: " << weight << "Kgn";
}
你的代码中没有这样的问题,它在Visual Studio中成功编译。 有时在线编译器可能真的有问题。如果需要在Codecademy中成功遵守它,您可以发送指向该Codecademy任务的链接以获得更准确的答案。
通常,当您在if语句后放置分号(;)时,您会收到此错误代码。在这种情况下,编译器应该是问题所在。在Visual Studio 2019中,该程序没有问题。
相关文章:
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 错误:无效的预处理指令 #i 的意思是 #if?
- 为什么切换 if else 语句的顺序会导致错误?
- 为什么 if 语句在 Java 中"if(1)"给出错误,但在C++中成立?
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 在if constexpr中使用带参数包的概念时,升级到gcc 9后出现编译错误
- 编译器错误"在if语句中分配不兼容的类型"
- C++ if 语句错误
- 否则在新程序中没有以前的 if 错误
- 对'if constexpr'的错误理解
- C++带有 while 语句范围捕获的 if 语句中的错误
- 将子字符串与字符串数组进行比较的 IF 条件会在C++中产生错误
- c++ IF 语句执行,但添加 ELSE 会显示错误
- 在 constexpr-if 条件下比较 constexpr 函数参数会导致错误
- C++ 带有 if 语句的家庭作业编译错误
- 为什么使用 and 运算符比较 if 语句中的 2 个对象会抛出错误,而使用 2 if 语句则不会
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- SDL 应用程序错误:“if”之前应为非限定 id
- 第 48 行和第 62 行 C++错误 if之前的预期非限定 id