C#使用循环的复利计算器(错误CS0103)

  • 本文关键字:计算器 错误 CS0103 循环 c#
  • 更新时间 :
  • 英文 :


我想在C#中使用不同名称空间中的两个类创建一个复利计算器,但我一辈子都无法弄清楚为什么我会不断出错。

PSA我是一个初学者,我知道这个代码可能看起来很糟糕,但请友善。

这是CompoundTest.cs


namespace CompoundTest
{
class Program
{
static void Main(string[] args)
{
CompoundClass newprogram = new CompoundClass();
Console.Write("nPlease enter the initial balance for your account: ");
double balance = Convert.ToDouble(Console.ReadLine());

Console.Write("nPlease enter the annual interest rate: ");
double interestRate = Convert.ToDouble(Console.ReadLine()) / 100;

Console.Write("nHow many years will you acrue interest? ");
double annualAmount = Convert.ToDouble(Console.ReadLine());

Console.WriteLine($"Your balance after {annualAmount} years is {accountBalance:C}");

Console.ReadLine();
}
}
}

这是Compound.cs


using System;
namespace Compound
{
public class CompoundClass
{
private double balance;
public int value { get; private set; }
public CompoundClass()
{
Balance = value;
}

public double Balance
{
get
{
return balance;
}
private set
{
if (value > 0)
{
balance = value;
}
}
}
public void Rate(double interestRate)
{
interestRate = value / 100;
}

public void Years(double annualAmount)
{
annualAmount = value * 12;

}

public void addMethod(double accountBalance)
{
for (int i = 1; i < annualAmount + 1; i++)
{
accountBalance = balance * Math.Pow(1 + interestRate / annualAmount, annualAmount * i);
}
}
}
}

我得到错误:

CS0103 C# The name '..' does not exist in the current context - in the public void addMethod(double accountBalance) method

您没有在ComposundClass上存储任何数据,方法

public void Rate(double interestRate)
{
interestRate = value / 100;
}

仅对函数范围内的输入参数利率进行操作,之后计算结果将丢失

如果你想在CompoundClass的整个生命周期中重用一个变量,那么把它定义为一个成员变量,比如:

private double _interestRate

并将您的功能更改为

public void Rate()
{
_interestRate = value / 100;
}

对于年度金额以及

private double _annualAmount;
public void Years()
{
_annualAmount = value * 12;
}

以及您对的计算

public double addMethod(double accountBalance)
{
for (int i = 1; i < annualAmount + 1; i++)
{
accountBalance = balance * Math.Pow(1 + _interestRate / _annualAmount, _annualAmount * i);
}
return accountBalance;
}

这段代码不止一个错误。老实说,我还不确定我是否有什么接近你的问题。

using System;
namespace Compound
{
public class CompoundClass
{
private double balance;
public int value { get; private set; }
public CompoundClass()
{   
//Balance with a big B is nowhere in context
Balance = value;
}

public double Balance
{
get
{
return balance;
}
private set
{
if (value > 0)
{
balance = value;
}
}
}
//As remarked by somebody else, this function does nothing. Without return or out parameter, interest rate will stay at nothing.
public void Rate(double interestRate)
{
interestRate = value / 100;
}
//The naming of this variable is bad. Did you mean "Amoung of Months"?
//Also as someone else pointed out, you do not return or otherwise persist this value
public void Years(double annualAmount)
{
annualAmount = value * 12;

}
//Method does not return anything.
//accountBalance is a local value and will not persist
public void addMethod(double accountBalance)
{
for (int i = 1; i < annualAmount + 1; i++)
{
//Avoid putting that much stuff into 1 line. It really messes with your ability to debug
//1-2 operations + 1 assignment to a temporary variable per line
//Anything more and you will have serious issues debugging this
accountBalance = balance * Math.Pow(1 + interestRate / annualAmount, annualAmount * i);
}
}
}
}

通常,它所使用的变量应该是纯参数(这意味着它应该是一个具有静态函数的静态类(,或者主要是类变量。你把这两样东西弄得到处都是。

最新更新