折扣码..我哪里错了?
此代码旨在根据客户数量给出物品的最终价格。我已经添加了方程式,但是当它运行时,折扣和最终价格没有正确评估。如果有人知道我们在伪代码中出错的地方,这将会非常有帮助。折扣码..我哪里错了?
using namespace std; int main() {
// Variables
double Retail, quantity, discount1, discount2, discount3, discount4, TotalCost1, TotalCost2, TotalCost3, TotalCost4;
Retail = 99;
quantity = 0;
discount1 = (quantity * Retail) * .20;
discount2 = (quantity * Retail) * .30;
discount3 = (quantity * Retail) * .40;
discount4 = (quantity * Retail) * .50;
TotalCost1 = quantity - discount1;
TotalCost2 = quantity - discount2;
TotalCost3 = quantity - discount3;
TotalCost4 = quantity - discount4;
//Equations
cout << "Please enter the quantity of items you would like to purchase: " << endl;
cin >> quantity;
cout << "The number of items being purchased is: " << quantity << endl;
if (quantity <= 19) {
cout << "Your final price is: " << discount1 << endl;
}
if (quantity >= 20) {
cout << "Your final price is: " << discount2 << endl;
}
if (quantity >= 50) {
cout << "Your final price is: " << discount3 << endl;
}
if (quantity >= 100) {
cout << "Your final price is: " << discount4 << endl;
}
system("pause");
}
回答:
难道只是你没有被定义为除了0您计算前的折扣什么数量?我在计算之前移动了您的购买内容并获得了更好的结果(使用在线编译器)。
double Retail, quantity, discount1, discount2, discount3, discount4, TotalCost1, TotalCost2, TotalCost3, TotalCost4; Retail = 99;
quantity = 0;
//Equations
cout << "Please enter the quantity of items you would like to purchase: " << endl;
cin >> quantity;
cout << "The number of items being purchased is: " << quantity << endl;
discount1 = (quantity * Retail) * .20;
discount2 = (quantity * Retail) * .30;
discount3 = (quantity * Retail) * .40;
discount4 = (quantity * Retail) * .50;
TotalCost1 = quantity - discount1;
TotalCost2 = quantity - discount2;
TotalCost3 = quantity - discount3;
TotalCost4 = quantity - discount4;
if (quantity <= 19) {
cout << "Your final price is: " << discount1 << endl;
}
if (quantity >= 20) {
cout << "Your final price is: " << discount2 << endl;
}
if (quantity >= 50) {
cout << "Your final price is: " << discount3 << endl;
}
if (quantity >= 100) {
cout << "Your final price is: " << discount4 << endl;
}
Please enter the quantity of items you would like to purchase:
The number of items being purchased is: 19
Your final price is: 376.2
以上是 折扣码..我哪里错了? 的全部内容, 来源链接: utcz.com/qa/257890.html