[英]Configuring Compound Interest Using a C# Loop in Different .cs Files?
我是 C# 課程的學生,並且有一項任務來確定復利。 但是,它需要使用循環和兩個單獨的.cs 文件來計算。
這是我最初擁有的,但它不在循環中並且全部在一個文件中:
using System;
namespace CompoundTest
{
class CompoundTest
{
static void Main()
{
double amount, rate, years, total;
Console.Write("\nPlease enter the initial balance for your account: ");
amount = double.Parse(Console.ReadLine());
Console.Write("\nPlease enter the annual interest rate: ");
rate = double.Parse(Console.ReadLine());
Console.Write("\nHow many years will you acrue interest? ");
years = double.Parse(Console.ReadLine());
total = amount * Math.Pow(1 + rate / 100, years);
Console.WriteLine($"Your balance after {years} years is {total:C}");
Console.ReadLine();
}
}
}
class 需要一個“addMonthlyInterest”方法,需要在 Main 中創建一個“帳戶”object 並在循環中調用它以增加每個月的利息。 請幫忙!
編輯文件 1(CompoundTestTwo.cs)
using System;
{
class CompoundTestTwo
{
static void Main()
{
Console.Write("\nPlease enter the initial balance for your account: ");
amount = double.Parse(Console.ReadLine());
Console.Write("\nPlease enter the annual interest rate: ");
rate = double.Parse(Console.ReadLine());
Console.Write("\nHow many years will you acrue interest? ");
years = int.Parse(Console.ReadLine());
Console.WriteLine($"Your balance after {years} years is {total:C}");
Console.ReadLine();
}
}
文件 2 (Compound.cs)
using System;
class Compound
{
public Compound(double rate,
double amount,
int years)
{
double total = 0;
for (int i = 1; i <= years; i++)
{
total += Math.Pow((1 + rate), i);
}
total *= amount;
return total;
}
}
我如何讓班級互相認識?
我不太明白 object “帳戶”是什么。 顯然,您的信息不夠詳細。 如果要計算復利,可以參考下面的代碼段。
double calculate(double rate, double amount, int years)
{
double total = 0;
for (int i = 1; i <= years; i++)
{
total += Math.Pow((1 + rate), i);
}
total *= amount;
return total;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.