繁体   English   中英

C#语法错误-

[英]C# syntax error -

我不太确定为什么在calcButton事件处理程序中的1 + annIntRate和numYears变量中出现错误。 我为两个变量获取的错误是“无法从十进制双精度转换”,但两个都应为十进制。 请帮忙。

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 Present_Value
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        //  Using the InputIsValid method to convert the users input and stores
        // it in the arguments (passed by reference).  If the conversion
        // is successful, the method returns true.  Otherwise it returns false.
        private bool InputIsValid(ref decimal futureValue, ref decimal annIntRate, ref decimal numYears)
        {
            // Flag variable to indicate whether the input is good 
            bool inputGood = false;

            // Try to convert all inputs to decimal.
            if (decimal.TryParse(futureAmtTextBox.Text, out futureValue))
            {
                if (decimal.TryParse(yearsTextBox.Text, out numYears))
                {
                    if (decimal.TryParse(rateTextBox.Text, out annIntRate))
                    {
                        //All inputs are good.
                        inputGood = true;
                    }
                    else
                    {
                        // Display an error message for anIntRate
                        MessageBox.Show("Annual Interest Rate is invalid.");
                    }
                }
                else
                {
                    // Display  an error message for the numYears
                    MessageBox.Show("Years in Savings is invalid.");
                }
            }
            else
            {
                // Dislay an error message for future value
                MessageBox.Show("Future amount is invalid");
            }
            // Return the reulst.
            return inputGood;
        }



        private void calcButton_Click(object sender, EventArgs e)
        {
            // Variables for for the present, future value,
            // annual interest rate, and number of years
            decimal futureValue = 0m, annIntRate = 0m,  numYears = 0m;

            if (InputIsValid(ref futureValue, ref annIntRate, ref numYears))
            {
                // Calculate Present Value
               decimal presentValue = (decimal)futureValue / (decimal)Math.Pow((**1 + annIntRate**), **numYears**);

                presentValLabel.Text = presentValue.ToString("C");
            }


        }
    }
}

这可能为您解决问题

decimal presentValue = (decimal) futureValue / (decimal) Math.Pow((double) (1 + annIntRate), (double) numYears);

因为Math.Pow取(Double,Double)作为返回值,所以返回指定的数字并提高到指定的幂。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM