首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最后一行输出两个组合输出的问题?If else语句

最后一行输出两个组合输出的问题?If else语句
EN

Stack Overflow用户
提问于 2017-09-14 12:18:33
回答 1查看 34关注 0票数 0

我很难找到为什么我的程序在我的最后一个输出行输出了两次。

如果我尝试输入5和500,它可以工作,但是当我尝试为第二个输入使用一个更大的数字时,比如500000,我在最后一个输出行中得到了两个不同的输出。

下面是我的代码。我很确定这是我最后一条if else语句,但我看不出有什么问题。

任何帮助都将不胜感激。

代码语言:javascript
复制
#include <iostream>
using namespace std;

int main()
{
    double estimatedSales;
    double copyPrice;
    double netMade;
    double firstOp_net;
    double secondOp_net;
    double thirdOp_net;
    double cashBefore = 5000;
    double cashAfter = 20000;
    const double FIRST_RATE = .125;
    const double SECOND_RATE1 = .10;
    const double SECOND_RATE2 = .14;
    double bestPrice;


    cout << "Enter the price for a copy of your novel\n";
    cin >> copyPrice;
    cout << "Enter the estimated number of copies that will be sold\n";
    cin >> estimatedSales;

    netMade = copyPrice * estimatedSales;

    cout << "It is estimated that your novel will generate $" << netMade << " of revenue.\n" ;

    firstOp_net = cashBefore + cashAfter;
    secondOp_net = netMade * FIRST_RATE;

    if (estimatedSales <= 4000) {
        thirdOp_net = netMade * SECOND_RATE1;
    } else { 
        thirdOp_net = netMade * SECOND_RATE2;
    }

    cout << "Royalties you keep for Option 1 is $" << firstOp_net <<"\n";
    cout << "Royalties you keep for Option 2 is $" << secondOp_net <<"\n";
    cout << "Royalties you keep for Option 3 is $" << thirdOp_net <<"\n";

    if (firstOp_net > secondOp_net) {
        bestPrice = firstOp_net;
    } else {
        cout<< "Your best bet would be to go with your 2nd Option for $"<< secondOp_net;
    }
    if (bestPrice > thirdOp_net) {
        cout << "Your best bet would be to go with your 1st Option for $"<< firstOp_net;
    } else {
        cout<< "Your best bet would be to go with your 3rd Option for $"<<thirdOp_net;
    }

    return 0;
}

这就是我所得到的:

代码语言:javascript
复制
Enter the price for a copy of your novel
5
Enter the estimated number of copies that will be sold
500000
It is estimated that your novel will generate $2.5e+06 of revenue.
Royalties you keep for Option 1 is $25000
Royalties you keep for Option 2 is $312500
Royalties you keep for Option 3 is $350000
Your best bet would be to go with your 2nd Option for $312500Your best bet would be to go with your 3rd Option for $350000

这就是我所期待的:

代码语言:javascript
复制
Enter the price for a copy of your novel
5
Enter the estimated number of copies that will be sold
500000
It is estimated that your novel will generate $2.5e+06 of revenue.
Royalties you keep for Option 1 is $25000
Royalties you keep for Option 2 is $312500
Royalties you keep for Option 3 is $350000
Your best bet would be to go with your 3rd Option for $350000
EN

回答 1

Stack Overflow用户

发布于 2017-09-14 12:38:25

在计算版税时,你的代码的问题是最后一部分的逻辑。实际上它的逻辑很简单,你可以减少代码量。

以下是我的解决方案:

代码语言:javascript
复制
#include <iostream>
using namespace std;

int main()
{

double estimatedSales;
double copyPrice;
double netMade;
double firstOp_net;
double secondOp_net;
double thirdOp_net;
double cashBefore = 5000;
double cashAfter = 20000;
const double FIRST_RATE = .125;
const double SECOND_RATE1 = .10;
const double SECOND_RATE2 = .14;
double bestPrice;


cout << "Enter the price for a copy of your novel\n";
cin >> copyPrice;
cout << "Enter the estimated number of copies that will be sold\n";
cin >> estimatedSales;

netMade = copyPrice*estimatedSales;

cout << "It is estimated that your novel will generate $" << netMade << " of revenue.\n" ;

firstOp_net = cashBefore+cashAfter;
secondOp_net = netMade*FIRST_RATE;

if (estimatedSales <= 4000)
    { thirdOp_net = netMade*SECOND_RATE1;
    }
else { thirdOp_net = netMade*SECOND_RATE2;
    }

cout << "Royalties you keep for Option 1 is $" << firstOp_net <<"\n";
cout << "Royalties you keep for Option 2 is $" << secondOp_net <<"\n";
cout << "Royalties you keep for Option 3 is $" << thirdOp_net <<"\n";

double big = firstOp_net;

if(big<secondOp_net)
{
    big = secondOp_net;
}
if(big<thirdOp_net)
{
    big = thirdOp_net;
}


    cout << "Your best bet would be to go with your 1st Option for $"<< big;
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46210451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档