簡體   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