c# - 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));
}
}
}
解决方案
您需要告诉控制台在哪里显示 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}");
推荐阅读
- laravel - Laravel New Project - Permission Denied
- android - QuickMatch onJoinedRoom 出现错误 2
- java - 如何在java中更改对象类型
- neo4j - 在 neo4j 中格式化时间持续时间
- selenium - 在 DDEV 容器中使用 Behat Drupal Extension 运行 selenium 测试
- java - 415 通过 REST 模板发送 json 文件时不受支持的媒体类型
- machine-learning - 如何在几天内训练神经网络?
- python - 无法移动文件python
- google-apps-script - 锁定预填充的 URL 字段
- android - 物理键盘在 Android 模拟器上输入错误的字符