對於此任務,我需要使用BankAccount程序做一些事情,但首先我需要複製運行的示例。我已經完全按照下面的截圖所示從作業頁中複製代碼,但是我得到如下所示的錯誤。無法從雙倍轉換爲十進制錯誤
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
我不知道是什麼導致了這些錯誤,因爲我不認爲我已經使用了雙旦,以及錯誤的非常快速的谷歌並沒有幫助我。
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);
}
}
}
請將您的代碼發佈到問題的正文中,而不是作爲圖片。同時告訴我們錯誤在代碼中發生的位置,所以我們不必試圖找出事情發生的地方。 – 2014-11-04 14:24:02
十進制文字用'm'後綴表示,因此如果構造函數需要'decimal'參數,則需要使用'新的BankAccount(142.50m)'。 – Lee 2014-11-04 14:25:44
我敢打賭你有一個函數,它返回一個double,你試圖傳遞給一個小數,沒有強制轉換。這就是說,我看不到你的代碼,因爲我的工作塊imgur。 – IllusiveBrian 2014-11-04 14:25:46