首页 > 解决方案 > C# 控制台应用程序以代码 0 结束,而不是提供它应该计算的输出

问题描述

该程序应该通过从用户那里获取几个输入然后将这些输入与复利公式一起应用来计算复利,但是虽然语法正确,但该程序除了输出计算值外,一切都是正确的。任何想法为什么会发生这种情况?

   using System;

   namespace compoundCalc
  {
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter investment sum:");
            int investment = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter annual interest rate:");
            double interestRate = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the number of times per year that interest is compounded per period:");
            int compoundNumber = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the number of periods the money is invested for:");
            int investmentPeriod = Convert.ToInt32(Console.ReadLine());
            double nt = Math.Pow((compoundNumber * investmentPeriod),(1+interestRate / compoundNumber ));
            double futureCapital = investment * nt;
            Console.WriteLine("The future value of your investment is:",  Convert.ToString(futureCapital));

        }
    }
}

标签: c#

解决方案


您需要告诉控制台在哪里显示 futureCapital 将其添加{0}到字符串的最后

Console.WriteLine("The future value of your investment is: {0}",Convert.ToString(futureCapital));

或者您可以使用字符串连接 +

Console.WriteLine("The future value of your investment is: " + futureCapital);

或更方便的使用字符串插值 $

Console.WriteLine($"The future value of your investment is:{futureCapital}");

推荐阅读