我收到此错误。无法将类型“ double”隐式转换为“ decimal”。存在明确的对话(您是否缺少演员表?)

马修·亨特

我收到此错误。无法将类型“ double”隐式转换为“ decimal”。存在显式对话(您是否缺少强制转换?)我对C#有点生锈。大约两年没有做太多

我要编写此代码。编写一个控制台应用程序,该应用程序根据申请状态和应纳税所得额计算所需的所得税额。该程序应提示用户输入单身或已婚(或退出)的备案状态。如果输入了其他任何内容,则将显示错误消息并重复输入。如果状态为“单身”或“已婚”,则输入应纳税所得额并打印税额。此过程应重复进行,直到输入Quit。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void gobutton_Click(object sender, EventArgs e)
        {
            decimal income  //hold income value
            decimal tax     //hold tax value  

            //get income value
            income = int.Parse(textBox2.Text);


            //Display the output ino the tax output label
            taxoutputlabel.Text = tax;

            if (textBox1.Text == "single")
                if (income <= 8700)
                    tax = income * 0.10;
                else if (income <= 35350)
                    tax = 4867.50 + (35350 - 8700) * 0.15;
                else if (income <= 85650)
                    tax = 17442.50 + (85650 - 35350) * 0.25;
                else if (income <= 178650)
                    tax = 43482.50 + (178650 - 85650) * 0.28;
                else if (income <= 388350)
                    tax = 112683.50 + (388350 - 178650) * 0.35;
            else if (textBox1.Text == "marriage, married")
                if (income <= 17400)
                        tax = income * 0.10;
                else if (income <= 70700)
                        tax = 4867.50 + (70700 - 17400) * 0.15;
                else if (income <= 142700)
                        tax = 17442.50 + (142700 - 70700) * 0.25;
                else if (income <= 217450)
                        tax = 43482.50 + (217450 -142700) * 0.28;
                else if (income <= 388350)
                        tax = 112683.50 + (388350 - 178650) * 0.35;
            else if (textBox1.Text == "divorced")
                if (income <= 8700)
                    tax = income * 0.10;
                else if (income <= 35350)
                    tax = 4867.50 + (35350 - 8700) * 0.15;
                else if (income <= 85650)
                    tax = 17442.50 + (85650 - 35350) * 0.25;
                else if (income <= 178650)
                    tax = 43482.50 + (178650 - 85650) * 0.28;
                else if (income <= 388350)
                    tax = 112683.50 + (388350 - 178650) * 0.35;

        }

        private void exitbutton_Click(object sender, EventArgs e)
        {
            //closes form
            this.Close();
        }
    }
}
雅库布·马萨德(Yacoub Massad)

double默认情况下,编译器将0.10(和任何其他分数)视为

要告诉编译器它是一个十进制数,您必须使用以下m后缀:

tax = income * 0.10m;

您必须对方程中使用的所有数字执行此操作。例如:

tax = 4867.50m + (35350m - 8700m) * 0.15m; 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档