簡體   English   中英

在不同的.cs 文件中使用 C# 循環配置復利?

[英]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;
        }

    }

我如何讓班級互相認識?

歡迎來到 StackOverflow。 這里的人們對幫助做作業有不同的感受,但我認為普遍的共識是他們反對。

您還沒有真正說明您需要幫助的確切內容,因此您應該嘗試更好地定義您的問題和 state 您到底需要什么幫助。

C# 文檔是一個很好的起點。 既然你說你需要另一個 class 你可以在這里閱讀如何創建類,類通常在不同的文件中創建。

您可能也需要 class 中的方法,因此您可以在此處閱讀有關方法的信息。

既然你說你需要一個循環,你可能會使用一個“for”循環,你可以在這里閱讀。

祝你的作業好運:) 真正學習的最好方法是自己學習,而不是讓別人為你拼寫出來!

我不太明白 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM