C#计算返回0

时间:2017-06-13 11:20:09

标签: c#

我是C#的新手,这段代码总是返回0.00,我不知道是什么原因。有人可以帮忙吗? 它是一个控制台程序,可能的输入是

sofia
1500

结果应为120.00

using System;

namespace TradeComissions
{
    class TradeComissions
    {
        static void Main(string[] args)
        {
            var town = Console.ReadLine().ToLower();
            var amount = double.Parse(Console.ReadLine());
            double result = 0.0;

            if(town == "sofia")
            {
                if (amount >= 0 && amount <= 500) 
                {                 
                    result = amount * (5 / 100);    
                }
                else if (amount >= 500 && amount <= 1000) 
                {         
                    result = amount * (7 / 100);    
                }
                else if (amount >= 1000 && amount <= 10000) 
                {       
                    result = amount * (8 / 100);    
                }
                else if (amount > 10000) 
                {                          
                    result = amount * (12 / 100);   
                }
            }

            Console.WriteLine("{0:f2}", result);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

你正在进行2个整数之间的数学除法,其中denom大于分子,例如5 / 100,该操作结果也为整数(零),而是:

result = amount * (5.0 / 100);

答案 1 :(得分:0)

这将有效:

static void Main(string[] args)
{
    var town = Console.ReadLine().ToLower();
    var amount = double.Parse(Console.ReadLine());
    double result = 0.0;

    if (town == "sofia")
    {
        if (amount >= 0 && amount <= 500)
        {
            result = amount * (0.05);
        }
        else if (amount >= 500 && amount <= 1000)
        {
            result = amount * (0.07);
        }
        else if (amount >= 1000 && amount <= 10000)
        {
            result = amount * (0.08);
        }
        else if (amount > 10000)
        {
            result = amount * (0.12);
        }
    }
    double f = result;

    Console.WriteLine("{0:f2}", result);
    Console.ReadKey();
}