C++If语句逻辑
C++ If statement logic
我目前正在上c++的一门课,由于某种原因,我们的一项作业遇到了一些麻烦。
询问用户在商店中的购买总额根据以下规则计算折扣:
- 如果他们花50美元,给他们10%的折扣
- 如果他们消费75美元,折扣为15%
- 如果他们消费100美元,折扣为25%
- 最后,如果他们花费250美元或更多,他们可以享受40%的折扣。
- 显示他们的购买金额,减去折扣并显示结果
这是我迄今为止写的代码:
#include <iostream>
using namespace std;
int main() {
double amount,discount=0.0;
cout<<"Enter the total amount of your purchase: $";
cin>>amount;
cout<<endl;
if (amount<50)
cout<<"You do not recieve a discount"<<endl;
else if(amount<=50.0)
{
discount=0.1*amount;
cout<<"Discount: $"<<discount<<endl;
amount=amount*0.9;
cout<<"Total Amount: $"<<amount<<endl;
return 0;
}
else if(amount<=75.0)
{
discount=0.15*amount;
cout<<"Discount: $"<<discount<<endl;
amount=amount*0.85;
cout<<"Total Amount: $"<<amount<<endl;
return 0;
}
else if(amount<=100.0)
{
discount=0.25*amount;
cout<<"Discount: $"<<discount<<endl;
amount=amount*0.75;
cout<<"Total Amount: $"<<amount<<endl;
return 0;
}
else if(amount<=250.0)
{
discount=0.4*amount;
cout<<"Discount: $"<<discount<<endl;
amount=amount*0.6;
cout<<"Total Amount: $"<<amount<<endl;
return 0;
}
return 0;
}
我没有收到正确的数字,例如,如果我输入74,它会给我11.1我觉得这是我打错号码的一个简单原因,但我不确定
让我们看看您的一个问题案例:
else if(amount<=50.0)
10%的折扣适用于消费至少50美元的人——上面使用的测试将其限制在消费最多50美元的人群(暂时忽略消费低于50美元的群体已经被之前的if
过滤掉)。
要创建一个if
子句来决定一个人是否应该享有10%的折扣,您需要对数学表达式进行建模:
$50.00 <= amount < $75.00
该表达式自然地转换为以下C代码:
else if (50.00 <= amount && amount < 75.00)
如果您愿意,您可以省略该表达式的第一部分——它由if
在以下else子句之前处理:
if (amount < 50.00)
cout<<"You do not receive a discount"<<endl;
else if (amount < 75.00)
// etc...
您的条件集与问题语句不匹配。当您请求74
时,它将与(amount <= 75)
相匹配,后者将应用错误的折扣。
你正在寻找的范围实际上是:
if (amount < 50) {
// No Discount
}
else if (amount < 75) {
// 10% Discount
}
else if (amount < 100) {
// 15% Discount
}
else if (amount < 250) {
// 25% Discount
}
else {
// 40% Discount
}
int main() {
double amount,discount=0.0;
cout<<"Enter the total amount of your purchase: $";
cin>>amount;
cout<<endl;
if (amount<50)
cout<<"You do not recieve a discount"<<endl;
else if(amount>=250.0)//250 or more
{
discount=0.4*amount;
cout<<"Discount: $"<<discount<<endl;
amount=amount*0.6;
cout<<"Total Amount: $"<<amount<<endl;
return 0;
}
else if(amount>=100.0)
{
discount=0.25*amount;
cout<<"Discount: $"<<discount<<endl;
amount=amount*0.75;
cout<<"Total Amount: $"<<amount<<endl;
return 0;
}
else if(amount>=75.0)
{
discount=0.15*amount;
cout<<"Discount: $"<<discount<<endl;
amount=amount*0.85;
cout<<"Total Amount: $"<<amount<<endl;
return 0;
}
else if(amount>=50.0)
{
discount=0.1*amount;
cout<<"Discount: $"<<discount<<endl;
amount=amount*0.9;
cout<<"Total Amount: $"<<amount<<endl;
return 0;
}
return 0;
}
float
和double
变量不应与==
、<=
、>=
进行比较,应使用epsilon。http://www.parashift.com/c++-faq/foating-point-arite.html
对我来说,你的if-else条件应该如下所示。
if (0 > amount)
{
// ERROR
return;
}
else if (50.0 > amount)
{
// No discount
return;
}
else if (75.0 > amount)
{
// 10% discount
return;
}
else if (100.0 > amount)
{
// 15% discount
return;
}
else if (250.0 > amount)
{
// 25% discount
return;
}
else
{
// 40% discount
return;
}
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- 转换器练习:跳过 if 语句和 if 语句
- if 语句与 if-else 语句,哪个更快
- C++ - 为什么我的 else 语句在 if-else 语句中被忽略了?
- Visual Studio C++ 完全忽略 if 语句或 if 语句中的行.VS 2015
- 事件是 SFML.切换语句 VS if 语句
- 嵌套IF语句vs IF- else
- switch语句或if语句消耗的资源少吗?
- if 语句 - 在 if 中分配指针 C++
- 有什么理由使用 switch 语句代替 if 和 elseif 的字符串