对于此分配,我需要对BankAccount程序进行一些处理,但首先需要复制运行示例的get。我已经完全按照下面的屏幕快照所示从工作分配表中复制了代码,但是出现以下错误。
Error 2 Argument 1: cannot convert from 'double' to 'decimal' Line 13 Column 51
Error 1 The best overloaded method match for 'BankAccount.BankAccount.BankAccount(decimal)' has some invalid arguments Line 13 Column 35
Error 4 Argument 1: cannot convert from 'double' to 'decimal' Line 13 Column 30
Error 3 The best overloaded method match for 'BankAccount.BankAccount.Withdraw(decimal)' has some invalid arguments Line 18 Column 13
我不知道是什么原因导致了这些错误,因为我不认为我曾经使用过两次,而且很快对这些错误的Google都没有帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BankAccount
{
class Program
{
static void Main(string[] args)
{
// Create Bank Account & Print Balance
BankAccount account = new BankAccount(142.50);
Console.WriteLine("Account Balance is: " + account.ToString());
// Withdraw £30.25
Console.WriteLine("Withdrawing £30.25");
account.Withdraw(30.25);
// Print balance again
Console.WriteLine("Account Balance is: " + account.ToString());
}
}
。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BankAccount
{
public class BankAccount
{
private decimal _balance;
public decimal Balance
{
get { return _balance; }
private set { _balance = value; }
}
//Constructor: Constructs a new Bank Account with 0 balance
public BankAccount()
{
Balance = 0;
}
//Constructor: Constructs a new Bank Account with the specified balance
public BankAccount(decimal balance)
{
Balance = balance;
}
//Deposits the specified amount into the Bank Account
public void Deposit(decimal amount)
{
Balance += amount;
}
//Withdraws the specified amount from the Bank Account
public void Withdraw(decimal amount)
{
Balance -= amount;
}
//ToString Override
public override string ToString()
{
return string.Format("{0}: Balance = {1}", "BankAccount", Balance);
}
}
}
这里:
BankAccount account = new BankAccount(142.50);
您正在传递double
文字142.50
。但是,BankAccount希望使用decimal
文字:
BankAccount account = new BankAccount(142.50m);
注意m
数字后面。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句