if/else if/else电话账单计算器
if/else if/ else phone bill calculator C++
我在c++类中编写电话账单程序时遇到了麻烦。问题出在if, else if和else语句的混乱中。我通过论坛以及stackoverflow上类似于我的其他程序,但无法理解我的错误在哪里。我的教授不擅长解释事情,到目前为止,我都是通过课本自学的。我知道这是一个非常业余的问题,但这就是它。
程序的目的是练习交互式输入、数学运算、"if…else"answers"if else if"链。
您需要输入一个电话套餐号码(1-3)。电话套餐选项如下…
Available Phone Plans:
1. Ultimate Unlimited talk and data Unlimited Text $75.00
2. Deluxe Unlimited talk, 3 GB data 550 Text messages $55.00
3. Basic Unlimited talk, 1 GB data 250 Text messages $35.00
对于方案2和方案3,每增加一条短信将收取0.20美元(20美分)的费用。
这些是我制作的变量..
string customerName, phoneNumber, planType;
int planChoice, textMessages;
float planCost, overTextTotal, untaxedTotal, taxedTotalDeduction,
taxedTotalBill, stateFeeDeduction, federalFeeDeduction;
float overTextFee = .20;
const float STATEFEE = .05;
const float FEDERALFEE = .02;
这些是计算结果//计算
untaxedTotal = planCost + overTextTotal;
stateFeeDeduction = untaxedTotal * STATEFEE;
federalFeeDeduction = untaxedTotal * FEDERALFEE;
taxedTotalBill = untaxedTotal + stateFeeDeduction + federalFeeDeduction;
cout<<"ntEnter 1 - 3 For Plan Type: ";
cin >> planChoice;
cout<<"nEnter Number Of Text Messages Used For The Month: ";
cin >> textMessages;
if(planChoice == 1 )
{
planType = "Ultimate";
planCost = 75.00;
overTextTotal = 0;
}
else if(planChoice == 2)
{
planType = "Deluxe";
}
if( textMessages <= 550)
{
planType = "Deluxe";
planCost = 55.00;
}
else
{
planCost = 55.00;
overTextTotal = ((textMessages - 550 ) * overTextFee);
}
if (planChoice == 3)
{
planType = "Basic";
}
if (textMessages <= 250)
{
planCost = 35.00;
}
else
{
planCost = 35.00;
overTextTotal = ((textMessages - 250 ) * overTextFee);
}
我的问题是我如何安排我的if, else if和else语句在我的输出中正确运行。
试着用下面的语句改变你的if-else语句:
if(planChoice == 1 )
{
planType = "Ultimate";
planCost = 75.00;
overTextTotal = 0;
}
else if(planChoice == 2)
{
planType = "Deluxe";
if( textMessages <= 550)
{
planType = "Deluxe";
planCost = 55.00;
}
else
{
planCost = 55.00;
overTextTotal = ((textMessages - 550 ) * overTextFee);
}
}
else if (planChoice == 3)
{
planType = "Basic";
if (textMessages <= 250)
{
planCost = 35.00;
}
else
{
planCost = 35.00;
overTextTotal = ((textMessages - 250 ) * overTextFee);
}
}
解释:您的原始代码没有将内部的if-else
语句包装在上部的else if
和else
花括号中。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- 使用if-else将数字转换为单词
- 为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
- C++ If/Else 语句被跳过
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 如何将 if else 语句重写为 switch 语句
- 在 C++ 中的 if-else if- else 语句期间更改变量
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 具有嵌套 if-else 的循环的时间复杂度
- 为什么无论输入如何,所有 if-else 语句都会打印?
- 简化对两个布尔值的 4 个 if/else 检查
- 如何在C++中创建 if else 循环而不是多个嵌套?
- 为什么切换 if else 语句的顺序会导致错误?
- 如何使用 if else 语句
- if/else 语句输出由于加号或减号而未显示正确的消息
- 如何解释 #if/#else 位域?(VC++菜鸟)
- if-else 语句仅按特定顺序工作,我不知道为什么