totalCost = 0,出了什么问题?

时间:2016-02-07 08:59:05

标签: c#

程序运行正常,但总是为totalCost返回零值。

这是代码

double primaryCost = 0.0;
double totalCost = 0.0;
Console.WriteLine("Hello! Please enter the price of your first item for purchase:");
string item = Console.ReadLine();
double purchaseItem = Convert.ToDouble(item);
if (purchaseItem >= 50.0)
{
    primaryCost = (purchaseItem / 100.0) * 10.0;
}

Console.WriteLine("Do you have additional items for purchase? (yes/no)");
string answer = Console.ReadLine();

if (answer == "yes")
{
    Console.WriteLine("Do you have additional items for purchase? (yes/no)");
    Console.WriteLine("Enter price for additional item: ");
    string additionalItem = Console.ReadLine();
    double secondaryItem = Convert.ToDouble(additionalItem);

    if (secondaryItem >= 50.00)
    {
        double secondaryCost = (secondaryItem / 100.0) * 10.0;
        double sum = secondaryCost + primaryCost;
        totalCost = sum;
    }
}
Console.WriteLine("The cost of everything being purchased is: ");
Console.Write(totalCost);//Value comes up as zero. What is the issue?
Console.ReadKey();

我哪里出错了?我不应该指定默认值0.0吗?

1 个答案:

答案 0 :(得分:1)

忽略一般计算成本的失败逻辑。对于您提供的代码:

加入:

// double primaryCost = 0.0;
double totalCost = 0.0;

double purchaseItem = Convert.ToDouble(item);
if (purchaseItem >= 50.0)
{
    totalCost = (purchaseItem / 100.0) * 10.0;
}

你可以完全摆脱primaryCost。

if (secondaryItem >= 50.00)
{
    double secondaryCost = (secondaryItem / 100.0) * 10.0;
    totalCost += secondaryCost;

}

... / ...

 <div class='row'>
     <div class='column' style='height:150px;width:150px;float:right;'>
       <P>some data</div>
     </div>
     <div class='column' style='height:150px;width:150px;float:right;'>
       <P>some data</div>
     </div>
     <div class='column' style='height:150px;width:150px;float:right;'>
       <P>some data</div>
     </div>
       <div class='column' style='height:150px;width:150px;float:right;'>
       <P>some data</div>
     </div>
     <div class='column' style='height:150px;width:150px;float:right;'>
       <P>some data</div>
     </div>
   </div>