简体   繁体   中英

Configuring Compound Interest Using a C# Loop in Different .cs Files?

I am student in a C# course and have an assignment to determine the compound interest. However it needs to be calculated using a loop and in two seperate.cs files.

Here's what I initially had but it's not in a loop and all in one file:


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();
        }
    }
}

The class needs an 'addMonthlyInterest' method, need to create an 'Account' object in Main and call it within a loop to add interest for each month. Please help!

EDIT File 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();
        }
    }

File 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;
        }

    }

How do I get the classes to recognize each other?

Welcome to StackOverflow. People on here have mixed feelings about helping with homework but I think the general consensus is they are against it.

You haven't really stated what you need help with exactly, so you should try and define your question better and state what it is exactly you need help with.

The C# documentation is a great place to start. Since you said you need another class you can read here on how to create classes, classes are usually created in different files.

You probably want a method in that class too, so you can read about methods here .

And since you said you need a loop you will probably use a 'for' loop, which you can read about here .

Good luck with your assignment:) the best way to actually learn is to learn yourself and not have someone spell it out for you!

I am not quite understand what the object "Account" is. Obviously, your information is not detailed enough. If you want to calculate the compound interest, you can refer to the following code segment.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM