制作二次根项目时,我的第二组嵌套 "IF" 语句被忽略
Making a Quadratic Roots project, my second set of nested "IF" statements is being ignored
为了快速解释,我需要六个不同的可能的 cout 输出。三个用于当 A = 0 时不起作用,然后另外三个用于当它不等于 0 时完美工作。我想我知道我的问题,'//A DOES 等于 0' 下的 if 语句集永远不会 运行。我的第二组嵌套 if 语句有什么问题?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double numA = 0;
double numB = 0;
double numC = 0;
int rootOne = 0;
int rootTwo = 0;
// User inputs roots
cout << "Program computes and prints the real roots of a quadratic polynomial a*x^2 + b*x + c." << endl;
cout << "Enter three real numbers a, b, c, seperated by spaces:";
cin >> numA >> numB >> numC;
//Calculating the Roots; (-b + sqrt(b * b - 4 * a * c)) / (2 *a) and (-b - sqrt(b * b - 4 * a * c)) / (2 *a)
rootOne = (-numB + sqrt(numB * numB - 4 * numA * numC)) / (2 * numA);
rootTwo = (-numB - sqrt(numB * numB - 4 * numA * numC)) / (2 * numA);
// A doesn't equal 0
if (numA != 0) {
if ((numB * numB) - (4 * numA * numC) > 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two distinct real roots" << endl;
cout << "root 1 = " << rootOne << " root2 = " << rootTwo << endl;
} else if ((numB * numB) - (4 * numA * numC) == 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two equal real roots" << endl;
cout << "root 1 = root2 = " << rootTwo << endl;
} else if ((numB * numB) - (4 * numA * numC) < 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two complex roots" << endl;
}
}
//A does equal 0
if (numA == 0) {
if ((numB == 0) && (numC != 0)) {
cout << "No roots for the constand function of " << numC << endl;
} else if ((numB == 0) && (numC == 0)) {
cout << "No roots for the degenerate case of 0 = 0." << endl;
} else {
cout << "The only root for the linear case of " << numA << "*x^2 + " << numB << "*x + " << numC << "is: " << numB << endl;
}
}
return 0;
}
糟糕:
if (numA =! 0) {
在这里你设置 numA
,到!0
(即true
,然后转换为1
作业)。
等于:
if (numA = !0) {
因此,您随后的 if
语句是 "being ignored" 也就不足为奇了:条件不再匹配。
如果您打开编译器警告,您会收到如下消息:
warning: suggest parentheses around assignment used as truth value
…然后您就不必猜测发生了什么要求我们为您修复您的代码。
"not equal to"运算符是!=
,不是=!
。
好的,所以我想谢谢你试图帮助......然后侮辱我,我从来没有学习过我的陈述是否正确,但无论如何。我最终只是重写了我的第一个嵌套语句并像我在底部所做的那样使用了逻辑运算符。所以我的新代码将所有六个代码都放在一个大巢中,并且有效!
The "not equal to" operator is !=, not =!
实际上我以为我来这里之前就在做,所以我确实学到了一些东西所以感谢那个人,因此,你赢得了最佳答案!
为了快速解释,我需要六个不同的可能的 cout 输出。三个用于当 A = 0 时不起作用,然后另外三个用于当它不等于 0 时完美工作。我想我知道我的问题,'//A DOES 等于 0' 下的 if 语句集永远不会 运行。我的第二组嵌套 if 语句有什么问题?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double numA = 0;
double numB = 0;
double numC = 0;
int rootOne = 0;
int rootTwo = 0;
// User inputs roots
cout << "Program computes and prints the real roots of a quadratic polynomial a*x^2 + b*x + c." << endl;
cout << "Enter three real numbers a, b, c, seperated by spaces:";
cin >> numA >> numB >> numC;
//Calculating the Roots; (-b + sqrt(b * b - 4 * a * c)) / (2 *a) and (-b - sqrt(b * b - 4 * a * c)) / (2 *a)
rootOne = (-numB + sqrt(numB * numB - 4 * numA * numC)) / (2 * numA);
rootTwo = (-numB - sqrt(numB * numB - 4 * numA * numC)) / (2 * numA);
// A doesn't equal 0
if (numA != 0) {
if ((numB * numB) - (4 * numA * numC) > 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two distinct real roots" << endl;
cout << "root 1 = " << rootOne << " root2 = " << rootTwo << endl;
} else if ((numB * numB) - (4 * numA * numC) == 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two equal real roots" << endl;
cout << "root 1 = root2 = " << rootTwo << endl;
} else if ((numB * numB) - (4 * numA * numC) < 0) {
cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two complex roots" << endl;
}
}
//A does equal 0
if (numA == 0) {
if ((numB == 0) && (numC != 0)) {
cout << "No roots for the constand function of " << numC << endl;
} else if ((numB == 0) && (numC == 0)) {
cout << "No roots for the degenerate case of 0 = 0." << endl;
} else {
cout << "The only root for the linear case of " << numA << "*x^2 + " << numB << "*x + " << numC << "is: " << numB << endl;
}
}
return 0;
}
糟糕:
if (numA =! 0) {
在这里你设置 numA
,到!0
(即true
,然后转换为1
作业)。
等于:
if (numA = !0) {
因此,您随后的 if
语句是 "being ignored" 也就不足为奇了:条件不再匹配。
如果您打开编译器警告,您会收到如下消息:
warning: suggest parentheses around assignment used as truth value
…然后您就不必猜测发生了什么要求我们为您修复您的代码。
"not equal to"运算符是!=
,不是=!
。
好的,所以我想谢谢你试图帮助......然后侮辱我,我从来没有学习过我的陈述是否正确,但无论如何。我最终只是重写了我的第一个嵌套语句并像我在底部所做的那样使用了逻辑运算符。所以我的新代码将所有六个代码都放在一个大巢中,并且有效!
The "not equal to" operator is !=, not =!
实际上我以为我来这里之前就在做,所以我确实学到了一些东西所以感谢那个人,因此,你赢得了最佳答案!