对于“ret”,我的输出总是为零。值为什么? [英] my output is always zero for "ret" value why?

查看:304
本文介绍了对于“ret”,我的输出总是为零。值为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;

namespace tax
{
    class Program
    {
        static void Main(string[] args)
        {
            float ret;
            Console.WriteLine("Enter gross pay");
            int gross = Convert.ToInt32(Console.ReadLine());
            Program n = new Program();
            ret = n.logic(gross);
            Console.WriteLine("Tax for your gross pay is {0}", ret);
            float total = gross + ret;
            Console.WriteLine("total amount to be paid is {0}", total);
            Console.ReadKey();



        }
        public float logic(int c)
        {
            float result;
            if(c <= 100000)
            {
                result= 0;
            }
            else if((c >100000)&&(c <=300000))
            {
               result =   (c  * (15 / 100));
            }
            else if(c >300000)
            {
               result =  (c  * (28 / 100));
            }
            else
            {
                result = 0.0f;
            }
            return result;
        }
    }
}

推荐答案

引用:

结果=(c *(15/100));

result = (c * (15 / 100));




引用:

结果=(c *(28/100));

result = (c * (28 / 100));



使用整数数学计算上述语句右侧的表达式。将它们更改为(例如)


The expressions on the right hand side of the above statements are evaluated using integer math. Change them to (for instance)

result =  (c  * (15.0f / 100));




result =  (c  * (28.0f / 100));



强制进行浮点评估。


to force floating point evaluation.


使用float而不是整数!

使用整数的错误是 15/100 = 0 以及 28/100 = 0



Use float instead of integer!
The error by using an integer is that 15 / 100 = 0 as well as 28 / 100 = 0!

public float logic(float c)
{
    float result;
    if(c <= 100000)
    {
        result= 0;
    }
    else if((c >100000)&&(c <=300000))
    {
       result =   (c  * (15f / 100f));
    }
    else if(c >300000)
    {
       result =  (c  * (28f / 100f));
    }
    else
    {
        result = 0.0f;
    }
    return result;
}


这篇关于对于“ret”,我的输出总是为零。值为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆